Java Self-study chapter

Source: Internet
Author: User

1. Variables and data Types

Variable concept: Use declarative means to keep each block in memory for use by the program.

Data type: integer, character, float, or other data type.

data types in Java can be divided into raw data types and reference data types.

The original data type is known as the base data type, with the most basic booleanbyte char short int long float doule

A reference data type is a special way of pointing to a variable's entity, similar to a C + + pointer. Variables of this type do not occupy memory space when declared, and must have additional memory space operations, such as array of strings belonging to this data type.

Constant variable Difference

Assign a constant to a variable, and the variable can be assigned repeatedly in the program.

2 . Types of variables in Java

Eight basic data types are used to store integers, floating-point numbers, characters, and Boolean values.

1, Integer type long,int,short, and byte

long64 bit of 8 bytes

int 4 bytes

short16 bit of 2 bytes

BYTE 8 -bit 1 bytes

the data type of the 1.1 constant

In Java, the data type of an integer constant is treated as an int , and if a constant greater than 217483647 is applied in the program, the compilation error occurs, and the instance 3.1

public class Javaint

{

publicstatic void Main (String args[])

{

Longnum = 329852547553; Define this one variable num, assign the constant number to this variable

System.out.println ("num=" +num);

}

}

1.2 The maximum and minimum values of the data type (max_value min_value) utility to output

public class Javavalue

{

publicstatic void Main (String args[])

{

Longlong_max = Java.lang.Long.MAX_VALUE;

Intint_max = Java.lang.Integer.MAX_VALUE;

Shortshort_max = Short.max_value;

Bytebyte_max = Byte.max_value;

Longlong_min = Long.min_value;

Intint_min = Integer.min_value;

Shortshort_min = Short.min_value;

Bytebyte_min = Byte.min_value;

System.out.println ("long_max=" +long_max);

System.out.println ("long_min=" +long_min);

System.out.println ("int_max=" +int_max);

System.out.println ("int_min=" +int_min);

System.out.println ("short_max=" +short_max);

System.out.println ("short_min=" +short_min);

System.out.println ("byte_max=" +byte_max);

System.out.println ("byte_min=" +byte_min);

}

}

1.3 overflow occurs, if the value of the variable is greater than the data type, the output will overflow when the variable refers to the data type maximum and the minimum value cycle.

2 character type (char)

the character type occupies 2 bytes in memory and can be used to hold characters such as English letters.

assigning values and characters to a character variable allows the program to run correctly. Note that the characters are enclosed in a pair of single quotation marks (' ').

This also involves the transfer of characters and ASCII table, hopefully through examples to explain this transfer character and ASCII code

public class Javaza

{

publicstatic void Main (String args[])

{

CHARCH1 = 64;

CHARCH2 = ' a ';

Charch3 = ' \ \ '; \ zhe Li You ge Zhuang yizi fu

CHARCH4 = ' \ ';

System.out.println ("ch1=" +ch1);

System.out.println ("ch2=" +CH2);

System.out.println ("ch3=" +CH3);

System.out.println ("ch4=" +CH4);

}

}

3 floating-point and double-precision floating-point numbers (float 4 bytes Double 8 bytes ) are the data types used to store decimals

Java uses the full name of the class to query the maximum and minimum values

Floatjava.lang.Float Max_value Min_value

Doublejava.lang.Double Max_value Min_value

4 Boolean Type Boolean true true false false

3 Conversion of data types in Java

1. Self-tuning conversion

automatic conversion meets two conditions

1.the data type before conversion is compatible with the converted type

2.the converted data type representation range is larger than the type before conversion

Note:

The conversion of a type is limited to that line statement, does not affect the type of the previously defined variable, and through automatic type conversion, can guarantee the accuracy of the data, It does not lose data content because of conversion. This type of conversion is also known as widening conversions.

conversions for individual data types: characters and integers can be automatically type converted, integers and floating-point numbers can also be automatically converted, only boolean and integers, and characters are incompatible.

Take a look at the features of auto-conversion

public class Javazh

{

publicstatic void Main (String args[])

{

INTA = 10;

FLOATB = 4.0f;

System.out.println ("a=" +a+ ", B =" +b ");

Systrm.out.println ("a/b=" + (A/b));

}

}

2. Forced conversion

when the operation of two integers, the result is an integer, if you want to get the calculated result is a floating-point number, the data type must be forced to convert, the conversion syntax:

(data type to convert) variable name; The purpose of the cast is shown below

public class JAVAQZ

{

publicstatic void Main (String args[])

{

INTA = 55,b = 9;

Floatg,h;

System.out.println ("a=" +a+ ", B =" +b ");

G= A/9;

System.out.println ("g=" +g);

H= A/(float) b;

System.out.println ("h=" +h);

}

}


This article is from the "Ops era" blog, please be sure to keep this source http://daomeng.blog.51cto.com/1655407/1795882

Java Self-study chapter

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.