This is a review of what I learned in class, as well as a notebook-like post where I write down ** help and improvement measures for my classmates. You can't make a practical application using Java, or you can't get the knowledge like "This function behaves like this!" Like the amazing people. Not bad. Don't swallow 100% more.
--Recommended environment: Visual Studio Code `Because Eclipse is heavy. ``
It's a good idea to include "terminal" and "Language Support for Java (TM) by Red Hat" as extensions. Other than that, I like it.
First of all, the customary Hello World!
java/hello.java
public class Hello
{
public static void main(String[] args)
{
System.out.println("hello World!");
}
}
At the beginning of Java, "public class Hello" is a required syntax. I thought it was magic. The important thing this time is "System.out.println (" Hello World! ");". You can use this ** println function to print characters to the console **. When handling a character string, it is necessary to enclose it in "" ". By the way, there is also a print function. As you can see by changing the syntax and re-executing, the difference is whether or not to start a new line after outputting Hello World !. Also, in Java ** you need to enter ";" at the end of the syntax **. If you do not enter it, you will not be able to convert the executable file and you will just get an error and exit, so hit it.
After entering the above syntax, open terminal (right-click to open it with VScode) and enter "javac Hello.java" on the terminal. Hit where the file is. When Hello.class is created, enter "java Hello". You don't have to hit .class. If you can say hello at the terminal safely, let's move on.
--println function --print function
Not limited to Java, when writing code, first write variables (declaration). The same thing comes out in almost every language.
java/Dec.java
public class Dec
{
public static void main(String[] args)
{
int num; //Integer 4-byte variable
num = 3; //Assign value
short num2 = 15; //Integer type 2-byte variable(I'm putting values at the same time)
boolean aaaa; //true or false
double bbbb; //8-byte double precision floating point number
float cccc; //4-byte double precision floating point number
char str; //Double-byte character
}
}
I still have it, but I'll omit it. I don't use it much in class ... Values can be assigned at the same time as the declaration, so there is no need to separate them.
--Basic type declaration
Here, a simple calculation, int type is converted to double type. First calculation
java/Math.java
public class Math
{
public static void main(String[] args)
{
int num1 = 12;
int num2 = 34;
answer = num1 + num2; //The order of calculation in programming is reversed!
//Because the calculated result is substituted
System.out.println(answer); //When outputting a variable """Do not add
}
}
When executed, it naturally becomes 46. This time it was easy because it was a calculation between int types, but it cannot be executed if double types are mixed. In Java, ** different types cannot be calculated **. The only way to avoid this is to convert the int type to a double type.
java/Cast.java
public class Cast
{
public static void main(String[] args)
{
int inum = 1;
double dnum = 2.5;
//The line below cannot be executed
//answer = inum + dnum;
answer = (double)inum + dnum; //Enter the type you want to convert in parentheses, calculate and execute!
System.out.println(answer);
}
}
The execution result is naturally 3.5. There are many 0s after the decimal point, but it is a specification. Be careful when assigning an int type to a small type such as a short type. Please note that it may be an unintended number.
--Calculation between types --Type conversion
From this point on, it becomes more like programming. The if statement is a statement that "if it isn't ...". Since you can set the conditions freely, the point is whether you can write the conditions according to your purpose.
java/If.java
public class If
{
public static void main(String[] args)
{
int num = 4;
if(num <= 5) //If num is 5 or less, execute the statement inside
{
System.out.println("The value of num is" + num + "So it was 5 or less.");
//When outputting statement and type values at the same time+Connect with
}
else //You can write a sentence that executes other than the above with else
{
System.out.println("Was a number greater than 5");
}
}
}
While the if statement looks at the conditions in order from the top, the switch statement makes it easier to see by creating a list.
java/Switch.java
public class Switch
{
public static void main(String[] args)
{
int num = 2;
switch(num)
{
case 0:
System.out.println("num is 0");
break; //Syntax to end the processing of switch statements
case 1:
case 2: //I am taking the specification that it will be executed in order if I do not hit the break statement
case 3:
System.out.println("0 < num < 4");
break;
default:
System.out.println("Greater than 3(suitable)"); //The default statement can describe how to execute a condition that does not apply to any of them.
}
}
}
There is only a "easy-to-read" difference between the switch statement and the if statement. Perhaps. If you want to write an if statement in a long way, the switch statement is better, and if there are only two conditions, you don't have to choose the switch statement. It would be reasonable to use the function that is easier to follow in your head.
--if statement --switch statement
When it gets longer, it's hard to see and write. Part2 and so on ...
Recommended Posts