JAVA programmers must read: Basics (3) language basics

Source: Internet
Author: User
Let's take a look at a specific example to give you a feeling of preemptible. The following is a BasicsDemo program. It ranges from 1 to 10 and displays the result:
Public class BasicsDemo {
Public static void main (String [] args ){
Int sum = 0;
For (int current = 1; current <= 10; current ++ ){
Sum + = current;
}
System. out. println ("Sum =" + sum );
}
}
The output of this program is:
Sum = 55
From the above example, we can see that even a small program uses many traditional features of the JAVA programming language, including variables, operators, and flow control statements. The above code may seem a bit complex, but once you finish learning this series of tutorials, you will find it actually very simple. This tutorial will teach you some basics of the JAVA programming language.
3.1 Variables
You can use variables in a program to hold data. This section describes the data type, how to initialize variables, and how to reference variables in code blocks. In fact, an object stores its state in a variable. A variable is a data item named by an identifier.
You must clear the name and type of each variable you want to use in your program. The variable name must be a valid identifier: a string of Unicode characters starting with a letter. You can use the variable name to reference the data contained in the variable. The type of this variable determines what type of value can be accommodated and what operations can be performed on it. To get a variable, type, and name, you must write the variable declaration as follows:
Type name
In addition to the name and type, you also need to give the variable a scope. The scope of a variable is determined by the position where the variable is declared.
The following MaxVariablesDemo program declares eight different types of variables:
Public class MaxVariablesDemo {
Public static void main (String args []) {
// Integer
Byte largestByte = Byte. MAX_VALUE;
Short largestShort = Short. MAX_VALUE;
Int largestInteger = Integer. MAX_VALUE;
Long largestLong = Long. MAX_VALUE;
// Number of instances
Float largestFloat = Float. MAX_VALUE;
Double largestDouble = Double. MAX_VALUE;
// Other data types: balanced and Boolean
Char aChar ='s ';
Boolean required lean = true;
// Display them
System. out. println ("The largest byte value is" + largestByte );
System. out. println ("The largest short value is" + largestShort );
System. out. println ("The largest integer value is" + largestInteger );

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.