Java Self-Learning introduction new experience 0.2

Source: Internet
Author: User

Java Basic data types

A variable is a request for memory to store a value, that is, when you create 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 major types of Java data types:

* Built-in data type

* Reference Data type

Built-in data types

The Java language provides eight basic types, six numeric types (four integers, two floating-point types). A character type,

There is also a Boolean type.

Byte

The *byte data type is a 8-bit, signed, Integer that is represented by a binary complement;

* Minimum value is-128 ( -2^7);

* Maximum value is 127 (2^7-1);

* 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;

* For example: byte a = +, byte B =-50.

Short

The *short data type is a 16-bit, signed integer that is represented by a binary complement.

* Minimum value is-32768 ( -2^15);

* Maximum value is 32767 (2^15-1);

The *short data type can also save space like Byte, where a short variable is one-second of the space occupied by the int type variable;

* Default value is 0;

* For example: short s = 1000. Short R =-20000.

Int:

The *int data type is a 32-bit, signed integer in binary complement notation;

* Minimum value is-2,147,483,648 ( -2^31);

* Maximum value is 2,147,483,647 (2^31-1);

* Generic integer variable defaults to int type;

* Default value is 0;

* For example: int a = 100000, int b =-200000.

Long

The *long data type is a 64-bit, signed integer in binary complement notation;

* Minimum value is-9,223,372,036,854,775,808 ( -2^63);

* 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;

* Default value is 0L;

* For example: long a = 100000L, long B = -200000l.

Float

The *float data type is single-precision, 32-bit, in accordance with the IEEE 754 standard floating-point number;

*float can save memory space when storing large floating-point group;

* Default value is 0.0f;

* Floating-point numbers cannot be used to denote precise values, such as currency;

* For example: float f1 = 234.5f.

Double

The *double data type is double-precision, 64-bit, in accordance with the IEEE 754 standard floating-point number;

* The default type of floating-point number is double type;

The *double type also cannot represent an exact value, such as currency;

* 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;

* For example: boolean one = True.

Char

The *char type is a single 16-bit Unicode character;

* Minimum value is ' \u0000 ' (i.e. 0);

* Maximum value is ' \uffff ' (i.e. 65,535);

*char data type can store any character;

* For example: char letter = ' A '.

Range of values:

 Public classPrimitivetypetest { Public Static voidMain (string[] args) {//byteSystem.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 (); // ShortSystem.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 (); //intSystem.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 (); //LongSystem.out.println ("base type: Long bits number:" +long.size); System.out.println ("Packing class: Java.lang.Long"); System.out.println ("min: long.min_value=" +long.min_value); System.out.println ("Maximum value: long.max_value=" +long.max_value);    System.out.println (); //floatSystem.out.println ("Basic type: float bits number:" +float.size); System.out.println ("Packing class: Java.lang.Float"); System.out.println ("min: float.min_value=" +float.min_value); System.out.println ("Maximum value: float.max_value=" +float.max_value);    System.out.println (); //DoubleSystem.out.println ("Basic type: Double bits number:" +double.size); System.out.println ("Packing class: Java.lang.Double"); System.out.println ("min: double.min_value=" +double.min_value); System.out.println ("Maximum value: double.max_value=" +double.max_value);    System.out.println (); //CharSystem.out.println ("base type: char bits number:" +character.size); System.out.println ("Packing class: Java.lang.Character"); //output Character.min_value to the console in numerical form rather than as a characterSystem.out.println ("min: character.min_value=" + (int) character.min_value); //output Character.max_value to the console in numerical form rather than as a characterSYSTEM.OUT.PRINTLN ("Maximum value: character.max_value=" + (int) (Character.max_value);}} 
Basic type:byteNumber of binary digits: 8Packing class: java.lang.Byte min: Byte.min_value=-128Maximum value: Byte.max_value=127Basic Type: ShortNumber of binary digits: 16Packing class: java.lang.Short min: Short.min_value=-32768Maximum value: Short.max_value=32767Basic Type:intNumber of binary digits: 32Packing class: Java.lang.Integer min: Integer.min_value=-2147483648Maximum value: Integer.max_value=2147483647Basic Type:LongNumber of binary digits: 64Packing class: Java.lang.Long min: Long.min_value=-9223372036854775808Maximum value: Long.max_value=9223372036854775807Basic Type:floatNumber of binary digits: 32Packing class: java.lang.Float min: Float.min_value=1.4e-45Maximum value: Float.max_value=3.4028235e38Basic Type:DoubleNumber of binary digits: 64Packing class: java.lang.Double min: Double.min_value=4.9e-324Maximum value: Double.max_value=1.7976931348623157e308Basic Type:CharNumber of binary digits: 16Packing class: java.lang.Character min: Character.min_value=0Maximum value: 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" means that the number before E is multiplied by 10

How many times. 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 the corresponding wrapper class java.lang.Void, but we cannot manipulate them.

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

types, such as Javatest, Firstjavatest, and so on. Once a variable is declared, the type cannot be changed.

* object. 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.

* For example: Animal Animal = new Animal ("Giraffe").

Java Constants

Constants are fixed values that do not need to be evaluated and represent the corresponding values directly.

A constant is a quantity that cannot be changed, and in Java a final flag is declared in a manner similar to a variable:

Final Double PI = 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, such as:

byte a =; char a = ' a ';

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

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

int decimal = +; int octal = 0144; int hexa = 0x64;

As in other languages, the string constants of Java are also sequences of characters that are contained within two quotation marks, and the following is an example of a string literal zi:

"Hello World" "Two\nlines" "\" This is in Quotes\ ""

Both string constants and character constants can contain any Unicode characters, such as:

char a = ' \u0001 '= "\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)

Java Self-Learning introduction new experience 0.2

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.