[Java] [Java] Appropriate introduction Java version by Tenpa people Part 1 (print,println)

4 minute read

About a proper introduction by people in Heaven

For those who have programming classes at school but don’t understand or are not good at it, or for those who want to be able to write code for the time being. You just have to do the details later. Therefore, there are some places where the terms are not proper. There is also a part that relies on feeling. If you are able to program to some extent in this series, please be sure to read ** books and class materials distributed by teachers at universities. **I think that the limit will come soon if you only know this series.

I think that you will understand somehow without looking at it, but I wrote a fairly basic and important thing. I would like you to go through the first look. https://qiita.com/Shodai-Kurasaki/items/b0137cf03ddd75485a6d

Main story

Display ### characters.

1-1

Execute the following sample code without saying anything. The file name is Sample1 and the extension is .java (for the future).

Sample1.java


class Sample1
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}

Result is

Hello World!

It will be displayed. Then immediately

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

Please pay attention to. This is the guy who is displaying the characters. I often use it. Then use something very similar to this. Try running the following sample code. The file name is Sample2.

Sample2.java


class Sample2
{
public static void main(String[] args)
{
System.out.print("abc");
System.out.println("def");
System.out.print("123");
}
}

There is one who doesn’t have “ln” in “System.out.print”. Let’s see the result.

abcdef
one two Three

Did you understand somehow? After displaying the character with “System.out.print”, there is no line break, but after the character displayed with “ln”, there is a line break. That is “System.out.print” really just prints the characters “System.out.println” starts a new line after displaying a character about it.

Check here 1-1 What is the execution result of the following program? (only in public naughty)

System.out.println("I am a genius");
System.out.print(".Honestly");
System.out.println("It doesn't matter");
System.out.println("Do you have tomorrow?");

I will write one response at the end, but if you do not understand it, please try it.

1-2

**About () System.out.println (here); A while ago, I used to put a sentence between “this chon chon”. However, depending on what you want to display, you may not need “”. Try running the following code.

Sample3.java


class Sample3
{
public static void main(String[] args)
{
// Hit a half-width slash like left, and the rest of the line has no effect on execution
// Can be used as a memo
It's a sequel.
System.out.println("1+1"); // 1st 1+1
System.out.println(1+1); // No. 2
It's a sequel.
System.out.println("a"); //a
System.out.println('a'); //a
}
}

First of all, the above two. You can display numbers without enclosing them in “”. However, if you want to display 1+1, you must write it as number 1. When Java is surrounded by “”, it considers that there is a character. In other words, in the first form, it is recognized as 1+1 characters. On the contrary, if you write the number 2, **1+1 can be regarded as a number (mathematical expression). Since it is **, the calculation result is displayed. Next are the two below. Unless it is a number (mathematical formula), the basic characters must be enclosed in “”, but if there is only one character, then there is no problem even if it is’’. Please think about it. There are no confirmation questions 1-2.

1-3

To display 1+1=2 with only the knowledge written up to 1-2

System.out.print("1+1=");
System.out.print(1+1);

I had to write. And here is Sample4. For the time being, please try to execute it. The execution result is written next to each line.

Sample4.java


class Sample4 // As you may already know, write the file name next to class
{
public static void main(String[] args)
{
System.out.println("1+1=" + 1+1); //1+1=11
System.out.println("1+1=" + (1+1)); //1+1=2
System.out.println("Aye" + "1+1" + "Eo"); // Aye 1+1 Eo
}
}

That’s right. If you connect a character (the one surrounded by “”) and a number with +, the number will be treated as a character. However, formulas that are enclosed only in () will be displayed after the calculation is complete. If you can understand it somehow, I think you can understand it in the future.

Confirmation question 1-3 What is the execution result of the following program? (only in public naughty)

System.out.println((20+4) + "3" + 2 + "is "+ "29" + ".");

I will write one response at the end, but if you do not understand it, please try it.

Exercises

Complete a program with the following execution results. In addition, although it is extremely inefficient, please calculate 501 for calculation.

result
Good morning. If you are attendance number 1, please come to room 501.

I won’t prepare the answer for this. Please do something about it. I can do it. Maybe maybe.

Answer to confirmation question

1-1

I am a genius
.. Really irrelevant
Do you have tomorrow?

1-3

2432 is 29.

Supplement If the problem was ((20+4) + 3 + “2”) this time, the addition will be done sequentially from the beginning, so it will be (27+”2”) and finally * It was displayed as *272**.

**Part 1 is over. Thank you for your hard work. **

If you find any mistakes, please let us know.