In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the basic knowledge points in Java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the basic knowledge points in Java".
"Hello World!"
Let's take a look at a HelloWorld.java program first. This program prints out a string of characters "Hello World!" on the screen:
Public class HelloWorld {public static void main (String [] args) {System.out.println ("HelloWorld!");}}
Some basic features of Java are included in the program:
Class (class): the above program defines a class HelloWorld with the same name as the .java file.
Method (method): a method main of the class is defined inside the class.
Statement (statement): the real "print" function is implemented by a statement, that is, System.out.println ("Hello World!")
Here are two points about how Java is written:
The statement in Java ends with; (the same as Candlestick +).
Use curly braces {} to integrate statements to form program blocks. Through the program block, we can know the scope of different parts of the program, such as where the class starts and ends.
Compile and run
The Java program must be compiled by the compiler before it can be executed. Under Linux or Mac, you can download and install Java JDK.
Compile using javac. Enter the following statement on the command line to compile:
$javac HelloWorld.java
Under the current path, a file named HelloWorld.class will be generated.
Use the java command to run. Java searches for the main method in this class and executes it.
$java HelloWorld variable
Computer languages usually need to store data in memory, such as variables in C, and Java has similar variables. Both Java and C are statically typed languages. Before using a variable, declare the type of the variable.
The variable (variable) occupies a certain amount of memory space. Different types of variables occupy different sizes. The variable types in Java are as follows:
Store size instance value comment
Byte 1byte 3 byt
Int 4bytes 3 integer
Short 2bytes 3 short integer
Long 8bytes 3 long integer
Float 4bytes 1.2 single precision floating point number
Double 8bytes 1.2 double precision floating point number
Char 2bytes 'a' character
Boolean 1bit true Boolean value
In Java, variables need to be declare before they can be used. In the declaration, I specify the type of variable and give it a special name so that it can be called later in the program. You can declare variables anywhere in the program. For example:
Public class Test {public static void main (String [] args) {System.out.println ("Declare in the middle:"); int a; a = 5; System.out.println (a); / / print an integer}}
The above an is the variable name. You can assign a value to a variable while declaring it, such as int a = 5
The concept of "variable" actually comes from a process-oriented programming language. In Java, so-called variables are actually "premitive type". We will go more in depth in the class explanation.
The above program can also see that / / leads comments are available in Java.
Array
There are arrays (array) in Java. The array contains multiple data of the same type. I declare an array of integers in the following way:
Int [] a
When declaring an array, the space required by the array is not really allocated to the array. I can use new to create the required space for the array at the same time as the declaration:
Int [] a = new int [100]
Here you create an array that can hold 100 integers. The corresponding memory allocation is also completed.
I can also assign values to the array while declaring. The size of the array is also determined.
Int [] a = new int [] {1,3,5,7,9}
Use int [I] to call the I subscript element of the array. I start at 0.
Other types of arrays are similar to integer arrays.
Expression.
An expression is a combination of variables, constants, and operators that represent a data. 1 + 1 is a common expression. Another example is:
Public class Test {public static void main (String [] args) {System.out.println ("Declare in the middle:"); int a; a = 5 + 1; System.out.println (a); / / print an integer}}
The above 5 + 1 is also an expression, which equals 6.
Mathematical expression
Mathematical operation, the result is a numerical value
1 + 2 addition
4-3.4 subtraction
7 * 1.5 multiplication
3.5 / 7 division
7% 2 find the remainder
Relational expression
Determine whether the expression is valid. That is, a Boolean value, true or false
A > 4.2 is greater than
3.4 > = b greater than or equal to
1.5
< 9 小于 6 1) || (2 == 1) or !true not 位运算 对整数的二进制形式逐位进行逻辑运算,得到一个整数 & and | or ^ xor ~ not 5 >1 0b110 right shift 1 bit
There are also the following operators that are common in C, which I will further explain when I use them:
Masking + variable m plus 1
N talk-variable n minus 1
Condition? X1: X2 condition is a Boolean value. According to condition, take the value of x1 or x2
Control structure
The syntax of the control flow in Java is similar to that of C. They all use {} to express affiliation.
Select (if)
If (conditon1) {statements;...} else if (condition2) {statements;...} else {statements;...}
The condition above is an expression that represents a true or false value. Statements; is a statement.
Practice writing a Java program to determine whether 2013 is a leap year.
Cycle (while)
While (condition) {
Statements
}
Cycle (do... While)
Do {
Statements
} while (condition); / / pay attention to the
Cycle (for)
For (initial; condition; update) {
Statements
}
To skip or jump out of a cycle.
In a loop, you can use the
Break; / / jump out of the loop
Continue; / / go directly to the next link
Practice writing a Java program that calculates from 1 plus 2, plus 3. All the way to the sum of 999
Select (switch)
Switch (expression) {
Case 1:
Statements
Break
Case 2:
Statements
Break
...
Default:
Statements
Break
}
object-oriented
"object" is a way for computers to abstract the world. "object-oriented" can be expressed in many ways. Here is an imprecise but more intuitive way of understanding:
Everything in the world can be called an object, such as Zhang San. Objects have identity (Identity), State (status) and Behavior (behavior).
The state of the object is represented by a data member (data member). Data members are also known as field. We use other objects as data members of that object. For example, an integer representing height, such as a nose.
The behavior of an object is represented by a member method (member method). We call it method for short. An object can have multiple methods, such as breathing and sleeping.
Objects can be classified (class), or classified into the same type (type). Objects of the same type have the same methods and data members of the same type. An object of a type is called an instance of that type (instance).
Class and object
Define the syntax of the class:
Class ClassName
{
Member1
Member2
...
}
We define a human class:
Class Human {void breath () {System.out.println ("hu...hu...");} int height;}
Within the scope of {}, the Human class has two members: a data member height and a method breath ().
The data member height is an integer type that can be used to store an integer.
The method represents what the object can do, that is, what the computer can do. Method can accept parameters and return a value. In the definition of breath (), there is a parameter list in the () after breath. Because the parameter list is empty, breath () does not accept parameters. The void before breath () is the type of return value, which means that breath does not return a value.
(the method is similar to a function in a process-oriented language)
Now let's create the object aPerson and call the object's method breath:
Public class Test {public static void main (String [] args) {Human aPerson = new Human (); aPerson.breath (); System.out.println (aPerson.height);}} class Human {void breath () {System.out.println ("hu...hu...");} int height;}
In the main method, create an object using the new keyword. Even for objects from the same class, each object occupies different memory, that is, the identity of the object is different.
Human aPerson declares that the aPerson object belongs to the Human class, which indicates the type of the object.
After the object is created, we can use the object. Data members refer to data members and use objects. Method () to call the method. Just as we print the aPerson.height later.
Thank you for your reading, these are the contents of "what are the basic knowledge points in Java". After the study of this article, I believe you have a deeper understanding of what are the basic knowledge points in Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.