Java Learning Diary the next day

Source: Internet
Author: User

Hello everybody good today is the next day

The last time simply wrote two programs, today continue to use the program to learn Java Foundation;

Today learning Java methods, give me feel equivalent to the function in C, if you do not know C, then you think of this method as a trick, can solve a problem such things;

In fact, the last time we wrote a method public static void Main (String args[]) is our main function, the entrance of our program,

Now we want to create a Java profect, click on the project and then right click on the new class in SRC, notice the class name (class name) first letter capitalized, if more than one word is the first letter capitalized,

This is the class we created, the class name cricle, we put this first tick, he gives us the main function, save us to knock.

At this time we go to write our method, our class name is circle, the meaning of the loop, we will be in the class to implement the Java inside the several loop statements;

Then we write our logical statement in our method Way1, i.e. the first loop while and the while loop output 1-5;

public static void Way1 ()
{
int i=0;
while (I<5)
{
i++;
System.out.println (i);
}

System.out.println ("I am finished with the value of I is not less than 5!! ");
}

So that we can write the method Way1, the method is written, we have to invoke the method, the method of invoking the notation

public static void Main (string[] args) {circle.way1 ();}

This time we click to run, or use the shortcut key F11, and then enter to see the results

This piece of code involves the knowledge points:

1. Permission to write for two days you also see, whether the method or the main function is public in front of the meaning, this is the right, specific Baidu go to it, I also contact said not clear (http://blog.csdn.net/itachiyang/article/ details/43647909) Slowly we understand,

2. The invocation of the method, we also see the invocation of the statement on that sentence, which is the wrapper of Java, the method is written in the class circle, using it to use the class circle to invoke it, and the point between the class and the method is equivalent to this meaning. Translation is the method of Cricle class Way1.

3. Return value void null, no return value .... Now that we know this, I'm going to get back to the various functions with return values.

4.while Loop statement, usage: while (condition) {}, the condition is true, in order to always execute the code in {}, from top to bottom, run exactly in the execution condition judgment, if the condition is true continue execution, do not meet the condition to jump out of the loop;

5.i++ equivalent to i=i+1; The self-increment operation called I, similar to ++i I----I when the plus sign in the left and in the right when the i++, or ++i alone into a sentence when there is no difference, the subsequent use of the difference to explain, it is said that i++ than i=i+1 the speed of operation,,,

Referring to the above method we write the rest of the loop method directly;

For loop

public static void Way2 () {for (int i=1;i<=5;i++) {System.out.println (i);} System.out.println ("I am finished with the value of I is not less than 5!! ");}

The calling method is called in the main function, as in the calling method one;

Running results you will find the same as the first, let's look at the syntax for the For loop

for (statement 1; Statement 2; statement 3) {} Statement 1 (initialization initial swap variant): variable initialization; statement 2 (condition conditional expression): Conditional Judgment statement; Statement 3 (Iteration): Change of Count {} Where logical statements are written

Also a loop does while is not the feeling and the first while the same, let's write method three;

public static void Way3 () {int I=1;do {System.out.println (i); i++;} while (i<=5); System.out.println ("I am finished with the value of I is not less than 5!! ");}

Syntax: do{logical statement} while (condition), is not with while the difference at a glance, the position of the condition and the logical statement is just upside down, good execution order is the same, do the first loop regardless of the condition is not satisfied, while the while doing the conditions to judge and then loop, with that cycle method , specific analysis of specific problems.

Java Learning Diary the next day

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.