[Java] Appropriate introduction by the people of Tempa Java Part 1 (print, println)

Appropriate introduction by Tenpa people

It is for people who have programming classes at school but are "not sure or not good at it" or who want to be able to write code for the time being. You can do the details later. Therefore, there are some places where the terms are not proper. There is also a part that depends on the feeling. If you can do some programming in this series, please read ** books and lesson materials distributed by teachers at universities. ** I think that the limit will come soon if only the knowledge of this series is used.

I think you can understand it without looking at it, but I wrote a fairly basic and important thing. I would like you to take a look. https://qiita.com/Shodai-Kurasaki/items/b0137cf03ddd75485a6d

Main story

Display characters and so on.

1-1 Run the following sample code without saying anything. The file name is Sample1 and the extension is .java (forever).

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 one who displays 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 a guy who doesn't have "ln" in "System.out.print". Let's see the result.

abcdef
123

Did you understand somehow? There is no line break after displaying the characters with "System.out.print", but there is a line break after the character displayed with "ln". In other words ** "System.out.print" really only prints characters "System.out.println" starts a new line after displaying characters ** about it.

Now here is the confirmation question 1-1 What is the execution result of the next program? (Only in public)

System.out.println("I'm a genius");
System.out.print(".. Really");
System.out.println("It doesn't matter");
System.out.println("What's your free time?");

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

1-2 ** About the inside of () ** System.out.println (here); I used to put a sentence in "this chompon". 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)
		{
			//If you hit a half-width slash as shown on the left, the rest of the line will not affect the execution.
			//Can be used as a memo
			
			System.out.println("1+1");	//No. 1 1+1
			System.out.println(1+1);	//No. 2 2
			
			System.out.println("a");	//a
			System.out.println('a');	//a
		}
}

First of all, the above two. Numbers can be displayed without enclosing them in "". However, if you want to display 1 + 1, you have to write like number 1. If Java encloses it in "", it considers it to be a character. In other words, in the first writing style, it is recognized as the character ** 1 + 1. On the contrary, in the second writing style, ** 1 + 1 is regarded as a number (mathematical expression). ** So the calculation result is displayed. Next are the two below. Basic characters must be enclosed in "" except for numbers (mathematical expressions), but when there is only one character, there is no problem with "'". Please think about it. There is no confirmation question 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. Please try it for the time being. The result of the execution 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("Ah" + "1+1" +"Eo");	 //Ah1+1Eo
		}
}

That's right. If you connect a letter (the one surrounded by "") and a number with +, the number will be treated as a letter. However, the formulas enclosed only in () will be displayed as characters after the calculation. If you understand it somehow, you will understand it as you use it in the future.

Confirmation problem 1-3 What is the execution result of the next program? (Only in public)

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

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

Exercises

Complete the program with the following execution result. Also, although it is extremely inefficient, please calculate 501.

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

I will not prepare the answer to this. Please do something about it. I can do it. Maybe maybe.

Confirmation question answer

1-1

I'm a genius
.. It doesn't really matter
What's your free time?

1-3

2432 is 29.

Supplement This time, if the problem is ** ((20 + 4) + 3 + "2") **, the addition will be done in order from the front, 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.

Recommended Posts

[Java] Appropriate introduction by the people of Tempa Java Part 1 (print, println)
[Java] Appropriate introduction by the people of Tempa Java Part 0 (Code rules)
Output of the book "Introduction to Java"
[Introduction to Java] List of things that got caught by the 14th day of programming
The part I was addicted to in "Introduction to Ajax in Java Web Applications" of NetBeans
Replace only part of the URL host with java
Traps brought about by the default implementation of the Java 8 interface
Summary of values returned by the Spliterator characteristics method #java
Server processing with Java (Introduction part.1)
The origin of Java lambda expressions
Check the domain by checking the MX record of the email address with java