Hello World Java Program

Source: Internet
Author: User

Learning Interest Group 437469371 This is a learning Java group of people welcome you to join the Welcome to discuss issues decline ads

As a programmer, the first program to come across is generally Hello world, and my first study is also learning Hello World,hello world is a display, no logic, on a simple display.

Program code:

Package First stage exercise. Day01;

Public class HelloWorld {

Public Static void Main (String []args) {

System. out. println ("Hello World");

}

}

Operation Result:

Package names are followed by packages, which is where our programs are located.

public class Each Java program can only have a public-modified class, this class generally makes our main class.

public static void Main (String []args)

If no access modifiers are written above the decorated element, the friendly is represented. S

Scope Current class same package descendant class other package

Public√√√√

Protected√√√x

Friendly√√xx

Private√xxx

Use of static

Static can be used to modify member methods of a class, member variables of a class, and you can write static code blocks to optimize program performance.

1.static method

Static methods are commonly referred to as static methods, and because they can be accessed without relying on any object, there is no this for static methods because it is not attached to any object, and since there are no objects, this is not the case. And because of this feature, non-static member variables and non-static member methods of the class cannot be accessed in a static method, because non-static member methods/variables must be dependent on a specific object to be able to be called.

It is important to note, though, that non-static member methods and non-static member variables cannot be accessed in static methods, but static member methods/variables are accessible in non-static member methods.

2.static variables

Static variables are also known as static variables, except that the static variables are shared by all objects, only one copy in memory, and are initialized only when the class is first loaded. Instead of static variables, which are owned by an object, are initialized when the object is created, there are multiple copies, and the replicas owned by each object do not affect each other.

The initialization order of static member variables is initialized in the order in which they are defined.

3 Static code block

Another key function of the static keyword is to form static blocks of code to optimize program performance. A static block can be placed anywhere in the class and can have more than one static block in the class. When the class is first loaded, each static block is executed in the order of the static blocks and is executed only once.

String args[] or string[] args represents an array of strings to the main method. and args is a string array of variable names, not keywords, is the abbreviation of the arguments, just a default name, is generally customary to write.

System. out. println ("Hello World");

This involves the use of a static keyword. For example, the system is actually a class within the Java.lang.

And out is a data member (also known as a field) inside the system, but this member is not the basic class, but the object of the Java.io.PrintStream class, and the Java.io.PrintStream class has some methods to wait and say. Let's take a look at the following, a data member or method that is modified by the static keyword can be referenced directly from the class name, data member, or class name. method, without having to establish the object first. So System.out is applied out of this static data member.

And out is a Java.io.PrintStream class object, so out must be able to call the Java.io.PrintStream class inside the method, println () is a method in the Java.io.PrintStream class, it is used to want the console output information.

Hello World Java Program

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.