First Lecture: Java basic Syntax (ii)

Source: Internet
Author: User

Ideal is to refer to the road star. There is no ideal, there is no firm direction, without direction, there is no life. --Leo Tolstoy


Content: Variables, constants, literal values, basic data types, assignments, annotations


One, variable (Variable)
The so-called variable is the amount of value that can be changed. When defining a variable, no special keyword adornments are required.

As an example:

public class Text {public static void main (string[] args) {String myname= "Liguojin"; myname= "Danmei"; System.out.println (MyName);}}
The printing result of the above program is "Danmei".



Second, constant (constant)
The so-called constant is the amount of the value that is not allowed to change. To declare a constant, use the final modifier of the keyword, in which the constants are all capitalized in accordance with the Java naming convention, and the words are separated by underscores:
Such as:

final int world=2;



Three, literal value (literal value)
Literal value isThe source code representation of some built-in data types in a programming language. Such as:

/       /integer value false    //boolean literal 3.1415//double literal value   ' a '      //char literal


Iv. basic Data type (Primitive type)
The data type in Java is divided into the basic data type (Primitive type) and the reference data type (Reference.
There are eight basic data types in Java:


1, integers (integer data type)
The Java language uses 3 in the method of representing integers, are decimal, octal and hexadecimal, usually we use 10 binary, sometimes also use 16, only because the fun will be used in octal. Here is a demonstration of three integer representations:

public class Text {public static void main (string[] args) {int i=15;//decimal Direct write int j=012;//octal front plus 0, octal with 0-7 for int k=0xb;//hex The front plus 0x or 0x,16 with 0-9 A-f said, here the a-f x case can System.out.println (i); System.out.println (j); System.out.println (k);}}
Type Bytes Default value Range
Byte 1 0 -128 ~ 127
Short 2 0 -32768 ~ 32767
Int 4 0 -2147483648 ~ 2147483647
Long 8 0L -9223372036854775808~9223372036854775807
The default type in integers is int, that is, an integer literal value if you don't explicitly specify his type, then he must be of type int.
Want to clearIf you declare an integer literal as a long integer, you need to use L or L as a suffix.

2. Floating point number (floating-point data type)

Type Bytes Default value Range
Float 4 0F 1.4E-45 ~ 3.4028235E38
Double 8 0D 4.9E-324 ~ 1.7976931348623157E308
Floating point numberThe default type is double, followed by the letter F or F, to cast it to float type.

if you want to define a single-precision floating-point literal, you must add F or f as a suffix. Floating-point numbers can of course be stored in integers.

Difference: Double precision is high, but memory consumption is two times, so double operation speed is much slower than float .


3. Boolean Type (Boolean)
Boolean expression a true or false, yes or no meaning. Using the Boolean keyword in Java to declare a variable to be a Boolean type, there are only 2 Boolean literals in Java: True and false. Note that it is all lowercase.

Type Bytes Default value Range
Boolean 1 False True,false


4, character type (char)
In Java, a single character in single quotation marks is used to represent a literal value.

Type Bytes Default value Range
Char 2 ' \u0000 ' ' \u0000 ' ~ ' \uffff '


Five, Assignment (Assignment)
"=" is an assignment operator, which, like the usage in mathematics, assigns the value on the right to the variable on the left.


Vi. Notes (annotation)
The annotations in the program are the means of communication between the programmer and the program Reader, and the code that is written to programmers. Typically, the compiler ignores the comment section and does not do a grammar check.

1. Good annotations can improve the readability of the software, allowing developers to understand the new code faster.
2. Good annotations can improve the cooperation efficiency of team development to a maximum.
3, the long-term annotation habit can exercise more rigorous thinking ability.

There are three types of annotations in Java:
Comment Line
/* Comment on several lines */
/** comment Several lines and write to Javadoc document */



This is where we go, take your time and enjoy it


First Lecture: Java basic Syntax (ii)

Related Article

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.