Basic data types for Java

Source: Internet
Author: User

A variable is a request for memory to store a value. In other words, when creating a variable, you need to request space in memory.

The memory management system allocates storage space for variables based on the type of the variable, and the allocated space can only be used to store that type of data.

Therefore, you can store integers, decimals, or characters in memory by defining variables of different types.

Two big data types for Java:

    • Built-in data types
    • Reference data type
Built-in data types

The Java language provides eight basic types. Six types of numbers (four integers (by default, int), two floating-point types (default is double), one character type, and one Boolean type.

Byte

    • The byte data type is a 8-bit, signed, integer represented by twos complement; (256 digits), 1 bytes
    • The minimum value is-128 ( -2^7);
    • The maximum value is 127 (2^7-1);
    • The default value is 0;
    • The byte type is used to save space in large arrays, mainly in place of integers, because the byte variable occupies only one-fourth of the int type;
    • Example: Byte a = 100,byte B =-50.

Short

    • The short data type is a 16-bit, signed integer in twos complement, representing 2 bytes
    • The minimum value is-32768 ( -2^15);
    • The maximum value is 32767 (2^15-1);
    • The short data type can also save space as Byte. A short variable is one-second of the space occupied by the int variable;
    • The default value is 0;
    • Example: Short s = 1000,short r =-20000.

Int:

    • The int data type is a 32-bit, signed integer represented by twos complement; 3 bytes
    • The minimum value is-2,147,483,648 ( -2^31);
    • The maximum value is 2,147,485,647 (2^31-1);
    • The generic integer variable defaults to the int type;
    • The default value is 0;
    • Example: int a = 100000, int b =-200000.

Long

    • The Long data type is a 64-bit, signed integer in binary complement; 4 bytes
    • The minimum value is-9,223,372,036,854,775,808 ( -2^63);
    • The maximum value is 9,223,372,036,854,775,807 (2^63-1);
    • This type is mainly used on systems that need to compare large integers;
    • The default value is 0L;
    • Example: Long a = 100000l,int B = -200000l.

Long a=111111111111111111111111 (error, Integer variable default is int type)

Long a=111111111111111111111111l (correct, cast)

Float

    • The float data type is single-precision, 32-bit, IEEE 754 compliant floating-point number, 4 bytes -3.4*e38-3.4*e38 ... A floating-point number is a rounding error
    • Float saves memory space when storing large floating-point groups;
    • The default value is 0.0f;
    • Floating-point numbers cannot be used to denote precise values, such as currency;
    • Example: float f1 = 234.5f.
    • Float f=6.26 (Error floating point number default type is double type)
    • Float f=6.26f (convert right, force)
    • Double d=4.55 (correct)

Double

    • The double data type is a dual-precision, 64-bit, IEEE 754-compliant floating-point number;
    • The default type of floating-point number is double type;
    • A double type cannot also represent an exact value, such as a currency;
    • The default value is 0.0d;
    • Example: Double D1 = 123.4.

Boolean

    • The Boolean data type represents one bit of information;
    • Only two values: true and false;
    • This type is only used as a sign to record the true/false situation;
    • The default value is false;
    • Example: Boolean one = True.

Char

    • The char type is a single 16-bit Unicode character, and a character is represented with ' '. The Unicode character set is used inside Java: He has some escape characters, 2 bytes
    • The minimum value is ' \u0000 ' (that is, 0);
    • The maximum value is ' \uffff ' (that is, 65,535); can be used as an integer, each of its characters corresponds to a number
    • Package com.bjsxt.basic;

      public class Testchar {

      public static void Main (string[] args) {
      TODO auto-generated method stubs
      Char C1 = ' a ';
      char c2 = ' on ';
      SYSTEM.OUT.PRINTLN (C1);
      System.out.println (C2);
      SYSTEM.OUT.PRINTLN (c1 + c2);
      }

      }

    • The output is:

      A
      On
      20075

    • Char data type can store any character;
    • Example: char letter = ' A '.

Char types can participate in integer calculations and then convert to character type

Package com.bjsxt.basic;

public class Testchar {

public static void Main (string[] args) {
TODO auto-generated method stubs
Char C1 = ' a ';
char c2 = ' on ';
SYSTEM.OUT.PRINTLN (C1);
System.out.println (C2);
SYSTEM.OUT.PRINTLN (c1 + c2);
int I=C1 + C2;
Char dd= (char) (i);
SYSTEM.OUT.PRINTLN (DD);
}

}

Output

A
On
20075

Instance

We do not need to force memory for the range of values of the base types of numeric types, because their values are already defined in the corresponding wrapper class as constants. Take a look at the following example:

Public Class Primitivetypetest { Public Static voidMain(String[]Args) { Byte System.Out.println("Base type: Byte bits number:" + Byte.SIZE); System.Out.println("Packing class: Java.lang.Byte"); System.Out.println("Min: byte.min_value=" + Byte.Min_value); System.Out.println("Maximum value: byte.max_value=" + Byte.Max_value); System.Out.println(); Short System.Out.println("Basic type: Short bits number:" + Short.SIZE); System.Out.println("Packing class: Java.lang.Short"); System.Out.println("Min: short.min_value=" + Short.Min_value); System.Out.println("Maximum value: short.max_value=" + Short.Max_value); System.Out.println(); Int System.Out.println("Base type: int bits number:" + Integer.SIZE); System.Out.println("Packing class: Java.lang.Integer"); System.Out.println("Min: integer.min_value=" + Integer.Min_value); System.Out.println("Maximum value: integer.max_value=" + Integer.max_value); System.out.println (); Long System.out.println ("Basic type: Long bits number:" + long.size); SYSTEM.OUT.PRINTLN ("Packing class: Java.lang.Long"); SYSTEM.OUT.PRINTLN ("Minimum value: long.min_value=" + long.min_value); SYSTEM.OUT.PRINTLN ("Maximum value: long.max_value=" + long.max_value); System.out.println (); Float System.out.println ("Basic type: float bits number:" + float.size); SYSTEM.OUT.PRINTLN ("Packing class: Java.lang.Float"); SYSTEM.OUT.PRINTLN ("Minimum value: float.min_value=" + float.min_value); SYSTEM.OUT.PRINTLN ("Maximum value: float.max_value=" + float.max_value); System.out.println (); Double System.out.println ("Basic type: Double bits number:" + double.size); SYSTEM.OUT.PRINTLN ("Packing class: Java.lang.Double"); SYSTEM.OUT.PRINTLN ("Minimum value: double.min_value=" + double.min_value); SYSTEM.OUT.PRINTLN ("Maximum value: double.max_value=" + double.max_value); System.out.println (); Char System.out.println ("base type: char bits number:" + character.size); SYSTEM.OUT.PRINTLN ("Packing class: Java.lang.Character"); Output Character.min_value to console System.out.println ("min: character.min_value=" +) in numeric form rather than as a character (int) Character.min_value); Output Character.max_value to console System.out.println ("Maximum: character.max_value=" + (int) character.max_value in numeric form rather than as a character ); } }

Compiling the above code output results in the following example:

Basic type:Byte Number of binary digits:8Packing class:Java.Lang.ByteMinimum value:Byte.Min_value=-128Maximum value:Byte.Max_value=127Basic type:Short Number of binary digits:16Packing class:Java.Lang.ShortMinimum value:Short.Min_value=-32768Maximum value:Short.Max_value=32767Basic type:Int Number of binary digits:32Packing class:Java.Lang.IntegerMinimum value:Integer.Min_value=-2147483648Maximum value:Integer.Max_value=2147483647Basic type:Long Number of binary digits:64Packing class:Java.Lang.LongMinimum value:Long.Min_value=-9223372036854775808Maximum value:Long.Max_value=9223372036854775807Basic type:Float Number of binary digits:32Packing class:Java.Lang.FloatMinimum value:Float.Min_value=1.4E-45Maximum value:Float.Max_value=3.4028235E38Basic type:Double Number of binary digits:64Packing class:Java.Lang.DoubleMinimum value:Double.Min_value=4.9E-324 Max: double.=1.7976931348623157e308 basic type: Span class= "KWD" >char  bits number: 16 wrapper class: java.. Character minimum: character.=0 Max:  Character. Max_value=65535          

The minimum and maximum values for float and double are output in the form of scientific notation, and the trailing "e+ number" indicates how many times the number before E is multiplied by 10. For example, 3.14E3 is 3.14x1000=3140,3.14e-3 is 3.14/1000=0.00314.

In fact, there is another primitive type Void in Java, which also has a corresponding wrapper class java.lang.Void, but we cannot manipulate them directly.

Reference type
    • Reference type variables are created by the constructors of the classes and can be used to access the referenced objects. These variables are specified at the time of declaration as a specific type, such as employee, Pubby, and so on. Once a variable is declared, the type cannot be changed.
    • objects, arrays are reference data types.
    • The default value for all reference types is null.
    • A reference variable can be used to refer to any type that is compatible with it.
    • Example: Animal Animal = new Animal ("Giraffe").
Java Constants

A constant is a fixed value. They do not need to be computed and represent the corresponding values directly.

Constant refers to the amount that cannot be changed. In Java, the final flag is declared in the same way as a variable:

FinalDouble=3.1415927;   

Although constant names can also be lowercase, uppercase letters are usually used to denote constants for easy identification.

Literals can be assigned to variables of any built-in type. For example:

byte=;  Char=' A '     

byte, int, long, and short can all be represented in decimal, 16-binary, and 8-binary ways.

When a constant is used, the prefix 0 represents 8, and the prefix 0x represents 16 binary. For example:

intDecimal=+;  int=0144;  int=0x64;           

Like other languages, a string constant in Java is also a sequence of characters that is contained between two quotation marks. The following is an example of a string literal:

"Hello World" "Two\nlines" "\" This is inquotes\ "" 

Both string constants and character constants can contain any Unicode characters. For example:

char=' \u0001 ';  String="\u0001";      

The Java language supports some special sequences of escape characters.

symbols character meaning
\ n Line Break (0x0a)
\ r Enter (0x0d)
\f Page Break (0x0c)
\b BACKSPACE (0x08)
\s Space (0x20)
\ t Tabs
\" Double quotes
\‘ Single quotation marks
\\ Back slash
\ddd octal character (DDD)
\uxxxx 16 Binary Unicode characters (xxxx)

Basic data types for Java

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.