Java Data types

Source: Internet
Author: User

Data types for Java

Java is a strongly typed language, which means that Java must declare a type for each variable. In Java, there are 8 basic data types and 3 reference data types.

    • Basic data types

   Integral type: byte short int long

The byte and short types account for 1 bytes and 2 bytes, respectively, and they are mainly used in specific situations, such as the underlying file processing or the need to control large arrays that occupy storage space. The int type is most commonly used, taking up 4 bytes and taking a range of 2,147,483,648 to 2,147,483,647. If you need to represent the number of people on the Earth, you need to use a long integer long,long type to occupy 8 bytes, typically adding a suffix l or l (such as 8000000000L) after the value that represents the long shape.

   Float type: float double

Floating-point types are used to represent numeric values that have a decimal part, and are divided into two kinds, float and double. The numeric precision of the data type represented by double is twice times the value of float, usually in the case of a double type in the writing procedure, and float is used only in rare cases. A value of type float has a suffix f or f (such as 3.14F), and a value without a suffix (such as 3.14) defaults to the double type.

   Character type: Char

Char variables are used to store Unicode-encoded characters, and Unicode-encoded characters take up to two bytes, so variables of type char are also two bytes.

   Boolean Type: Boolean

A Boolean (Boolean) type has only two values: True and false, which are used to determine the logical condition. It is important to note that numeric and Boolean variables cannot be converted to each other.

    • Reference data type

There are 3 types of reference data: Reference to class, reference to interface, and array reference

a reference to a class

a reference-type variable of a class points to an object created by the class that itself does not store the contents of the object, but simply refers to an object (which can be thought of as a pointer type in C). As shown below, the variable str is a reference to the string class that points to a string object with a value of "Hello", and we can manipulate the object it points to by using the STR variable.

 1  public  class   Main { 2  3  public  static  void   main (string[] args) { 4  5  string str = new  string ( "Hello"  6   System.out.println (Str.length ());  7  }  8  }        

references to interfaces

Similar to a reference type variable for a class, the reference to an interface points to an object created by the implementation class of the interface. The variable IClass points to the object created by the implementation class TestClass of the interface IClass, and we can manipulate the object it points to by the IClass variable.

1 Public classMain {2 3 Public Static voidMain (string[] args) {4 5 IClass IClass =NewTestClass ();6iclass.f ();7     } 8 } 9 10InterfaceIClass {11voidf ();12 }13 14classTestClassImplementsiclass{15@Override16 Public voidf () {System.out.println ("This is testclass!"); 18     }19}

References to arrays

The reference to the array points to an array created by the New keyword. We can insert, delete, modify, and manipulate arrays by reference variables.

1  Public classMain {2 3      Public Static voidMain (string[] args) {4         //create an array of integers with 5 elements5         int[] arr =New int[5]; 6         //assigning values to each element of an array7          for(inti=0; i<arr.length; ++i) {8Arr[i] = (i+1);9         }Ten     } One}

Java Data types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.