Introduction to Java Basics (1) Displaying Characters on the Screen

content

foreword

Features of Java

Two useful tips for idea

Display characters on the screen

source program and source file

 Compile and run the program

compile

run

Notes

Output and stream on screen

concatenation of strings 

 free writing

indentation

end


foreword

Shi Lu. The Java Programming Introduction series is very suitable for the reference of small partners who are just learning Java. I think that the basics are more detailed and can be used for reference and learning by the majority of Java beginners. There will be more similar programs in this series, but this is not long-winded. If you only tell the difference, I believe that many programmers do not know how to operate. There is the (num) next to the code segment, which is a point worth analyzing. If you think this blog is helpful for you to get started with Java, you can give some support and encouragement! Talking is useless, let's get to the point.

Features of Java

Java is a widely used language, its characteristics are: 1. Java development tools are available for free trial; 2. Write once, run everywhere; 3. Syntax similar to C/C++; 4. Strong typing, you can easily create programs with high reliability; 5. Support object-oriented programming; 6. Contains a large number of libraries; 7. Use garbage collection for memory management; 8. Exception handling and concurrent processing; 9. Use packages to classify classes.

Two useful tips for idea

Let's start with an appetizer , and look at the following code:

 public static void main(String[] args) {
        System.out.println();
    }

It doesn't matter if you don't understand the code mentioned above. Like the lengthy code above, if we have to type it every time we write a program, it's really a waste of time. Shi Lu. Let me share 2 tips for you :

Quickly generate main() method: psvm+enter
A quick way to generate output statements: sout+Enter

Display characters on the screen

A very important function of a computer is to convey information to people through characters and numbers . Next, I introduce the method of displaying characters on the console screen. Let’s start with the second piece of code in this series:

package Chap01;         //包     (1)

public class Hello {                       //类         (2)
    public static void main(String[] args) {     //方法        (3)
        System.out.println("第一个Java程序");      (4)(5)
        System.out.println("输出到画面上");
    }
}

(1) When the source program we want to implement is not very large, package Chap01 (package name, determined by the operator) can be omitted. The source program after omission does not belong to any package, it belongs to unnamed package;

(2) When we create a program, we map real objects (objects) and concepts to objects (variables) in the program. Please see the picture below:

 (3) When we just started learning Java, we temporarily focus on the main method, and when we talk about classes later, we will focus on other classes. As for why you need to fill in public, static, etc., we only need to know that this is a fixed format now. Just remember it, I will analyze it for you one by one later;

(4) System.out.println involves the knowledge of the automatic import of the Java.lang package, and its prototype can be written as java.lang.System.out.println("shilv'), which belongs to the content of the package and will be released in the near future. share with you in the future;

(5) The inside of () is a string constant, and the content that does not belong to this blog will be shared with you one by one in the future.

source program and source file

We create the program written above by character sequence, this kind of program is called source program , and the file used to save the source program is called source file . Therefore, the source file name of the program is Hello.Java , as shown in the following figure (the software used by the blogger is idea2021):

Creating a directory is also a knowledge, which allows us to better manage the code , so that we can find it immediately when we use it in the future. Here is a directory management recommended by Shi Lu :

 Compile and run the program

After the source program is written, it cannot be run directly. Before running, we should (1) compile the source program to generate bytecode; (2) run the generated bytecode. Roughly as follows:

 

compile

The so-called compilation is to convert the source program that cannot be directly run into a form that can be run. We can use the javac command to perform this operation. The compilation operation is as right: javac.Hello.java+Enter

run

After the compilation is successful, you can run the program. The java command will read the class from the class file and run it.

To say a little more, the source program cannot be run directly, you need to use the javac command to compile it into a class file, and use the java command to run the classes in the class file.

Notes

Like most programming techniques, Java also has annotations. What is a comment, that is, an explanation of the program. The content of the comment has no effect on the running of the program:

(1)传统注释 /*  ....  */
(2)单行注释 //
(3)文档化注释 /** .... */

Note that (3) documentation comments, like (1), can span multiple lines, such as the following:

package Chap01;        

public class Hello {                             
    public static void main(String[] args) {            
      /**  System.out.println("第一个Java程序");      
        System.out.println("输出到画面上");
        System.out.println("第一个Java程序");      
        System.out.println("输出到画面上");   
    */                这样我们将将四行代码注释掉啦
    }
}

In fact, there are more useful methods than annotations - annotations , which will also be shared with you in the near future.

Output and stream on screen

Java programs use streams (characters that flow like flowing river water) and console screens for external input and output operations.

   System.out 是与控制台画面所关联的流,称为标准输出流

As mentioned above, the functions of print and println are different. What is the function of the extra ln of println? And continue to look down:

public class Howareyou1 {
    public static void main(String[] args) {
        System.out.print("Hello!");
        System.out.println("How are you?");
    }
}

Take a look at his output again:

If you add ln after the print of the original code:

 

It can be seen that ln in println has the effect of newline! It is equivalent to the escape character \n.

concatenation of strings 

In Java, multiple string constants can be concatenated with the plus sign . Like the following lines of code:

public class Howareyou2 {
    public static void main(String[] args) {
        System.out.println("Hello!"+"How are you?");
    }
}

The output of the run looks like this:

Hello and How are you? It's magically stitched together!

Let's take another example in Chinese:

public class offer {
    public static void main(String[] args) {
        System.out.println("好好学习好好卷,"+"将来拿大厂offer。");
    }
}

The output of the run looks like this:

 free writing

Code in Java is free to write, and look at the following code:

public class
offer {
    public static
    void main(String[] args) {
        System       .      out.           //单词之前是没有空白的
                println("好好学习好好卷,"+"将来拿大厂offer。");   //字符串常量间是没有换行的
                    }
        }

Interested friends can try ctrl+c, the program can run successfully! But there are two points to note:

(1) Blanks cannot be added in the middle of words;

(2) The middle of a string constant cannot be a newline.

indentation

We copy the above code to observe and observe:

public class Howareyou2 {
    public static void main(String[] args) {
        System.out.println("Hello!"+"How are you?");
    }
}

When we enter characters, the compiler will automatically help us move the content to the right by a few characters , so that the structure of the program becomes clear at a glance. The left blank set for this is called indentation , and the description of indentation is used for abbreviation .

end

The above is Shi Lu. For the first blog in this series----Introduction to Java Basics (1) Displaying characters on the screen. Thank you for reading this far! If you think this blog is helpful to you who are learning Java, please give Shi Lu. more support and attention! It is not easy to create, thank you all with tears first! ORZ will share more practical Java basics and work with you in the next few months. I hope that next time I can provide you with more A good introduction to Java basics (2), and I hope that the next blog will have you!

Guess you like

Origin blog.csdn.net/qq_64263760/article/details/123420626