In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you about how Java changes from HelloWorld to object-oriented. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
"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:
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
Relational expression
Determine whether the expression is valid. That is, a Boolean value, true or false
Bollinger expression
The logical relationship between and, OR, and not of two Boolean values
Bit operation
The binary form of an integer is logically operated bit by bit to get an integer.
There are also the following operators that are common in C, which I will further explain when I use them:
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); / / Note the
Cycle (for)
For (initial; condition; update) {statements;}
To skip or jump out of a cycle.
In the loop, you can use break; / / to jump out of the loop continue; / / and go directly to the next ring to practice writing a Java program, calculating 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.
Java is a completely object-oriented language.
This is how the Java shared by the editor goes from HelloWorld to object-oriented. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.