[Java] Basic types and instruction notes

Data type

--Integer --byte: ± about 128 --short: ± about 32,000 --ʻInt: ± about 2.1 billion --long: ± 900 K --Decimal --float: Ambiguous and good decimals --double: Strict decimal --Authenticity value - booleantrueorfalse`

Type conversion concept

Follows the magnitude relationship of data types. Small can be put in large, but the opposite is not possible. byte < short < int < long < float < double

public class Main {
  public static void main(String[] args){
	//When calculating between int types-> 2
	System.out.println("When calculating between int types");
	int a=5;
	int b=2;
	System.out.println(a/b);

	//When calculating between int types-> 2.5
	System.out.println("When calculating between double types");
	double c=5;
	double d=2;
	System.out.println(c/d);

	//One is int type,When the other is calculated in double type-> 2.5
	System.out.println("One is int type,When the other is calculated in double type");
	int e=5;
	double f=2;
	System.out.println(e/f);

	//One is double type,When the other is calculated as an int type(verification of accounts) -> 2.5
	System.out.println("One is double type,When the other is calculated as an int type(verification of accounts)");
	double g=5;
	int h=2;
	System.out.println(g/h);

	//When the calculation between int types is assigned to the double type variable x-> 2.0
	System.out.println("When the calculation between int types is assigned to the double type variable x");
	int i=5;
	int j=2;
	double k=i/j;
	System.out.println(k);
  }
}

Basic statement

--Screen output --System.out.print (argument 1); --System.out.println (argument 1); --Comparison of numerical values --Math.max (argument 1, argument 2); --String type numeric type conversion --ʻInteger.parseInt (argument 1); --Random number generation --new java.util.Random (). nextInt (argument 1); --Accepting input from the keyboard --new java.util.Scanner (System.in) .nextLine (argument 1); --new java.util.Scanner (System.in) .nextInt (argument 1);`

public class test02 {
  public static void main(String[] args){
    //Screen output
    System.out.print("System.out.print();");
    String a="My name is";
    String b="sample program";
    System.out.print(a+b);
    System.out.println(a+b);

    //Comparison of numerical values;
    System.out.println("Math.max();");
    int m=5;
    int n=2;
    int l=Math.max(m,n);
    System.out.println("Comparative experiment:"+m+"When"+n+"And the bigger one"+l);
    System.out.println("--");

    //String type numeric type conversion;
    System.out.println("Integer.parseInt();");
    String age="31";
    int i=Integer.parseInt(age);
    System.out.println("You next year"+(i+1)+"I'm old");
    System.out.println("--");

    //Random number generation;
    System.out.println("new java.util.Random().nextInt();");
    int r=new java.util.Random().nextInt(2); //Put an argument in the direction of nextInt.
    System.out.println("Random number generator:"+r);
    System.out.println("--");

    //Accepting input from the keyboard
    System.out.println("new java.util.Scanner(System.in).next~system");
    //Receive name and age.
    System.out.println("Please tell me your name.");
    String x = new java.util.Scanner(System.in).nextLine();
    System.out.println("Please enter your age.");
    int y = new java.util.Scanner(System.in).nextInt();
    //output
    System.out.println("Welcome"+x+"Mr. ("+y+"age).");
  }
}

Postscript 2019/05/13

new java.util.Scanner (System.in) .nextline (), but It creates an instance and uses methods in one sentence.

--Instance: java.util.Scanner --Method: nextLine ()

But this causes Eclipse to return an error.

<unassingned Closeable value>Will never be closed

It seems that you should use the Scanner.close () method to end the input reception. So it's a good idea to split the instance creation and method usage into two statements. [(reference)] (https://ja.stackoverflow.com/questions/41408/java-%E3%83%AA%E3%82%BD%E3%83%BC%E3%82%B9-%E3%83%AA%E3%83%BC%E3%82%AFunassigned-closeable-value%E3%81%8C%E9%96%89%E3%81%98%E3%82%89%E3%82%8C%E3%82%8B%E3%81%93%E3%81%A8%E3%81%AF%E3%81%82%E3%82%8A%E3%81%BE%E3%81%9B%E3%82%93)

Scanner scn = new java.util.Scanner(System.in);
String str = scn.nextLine();
scn.close(); //Close here.

Or

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();

There is also such a way of writing ↑.

In addition, it seems that no error will occur if you hit javac and java directly at the command prompt.

Reference book

[Introduction to Java for a refreshing second edition] (https://www.amazon.co.jp/dp/B00MIM1KFC/ref=dp-kindle-redirect?_encoding=UTF8&btkr=1) Pp.075-091

[Easy Java 7th Edition] (https://www.amazon.co.jp/dp/4815600848/ref=cm_sw_r_tw_dp_U_x_P7o2CbMQG50SB) Pp.61-67

Recommended Posts

[Java] Basic types and instruction notes
Basic data types and reference types (Java)
Java basic data types and reference types
[Java] Exception types and basic processing
[Java] Variables and types
[Java] Basic method notes
Java basic data types
About Java basic data types and reference type memory
Java variable declaration, initialization, and types
Java Primer Series (Variables and Types)
java notes
Reading and writing Java basic files
About Java primitive types and reference types
Notes on Java path and Package
Java starting from beginner, variables and types
Java study # 3 (type conversion and instruction execution)
[Note] Cooperation between Java and DB (basic)
[Introduction to Java] Variable declarations and types
Java basic grammar
Java Generics (Notes)
Java basic grammar
[Java] Array notes
Java basic knowledge 1
[Java] Basic structure
[Java] [Basic] Glossary
Java basic grammar
Java and JavaScript
[Java] Study notes
XXE and Java
Java basic grammar
Java serialization notes
Java exercises [Basic]
Review notes for Java 1.7 and later file copies
[Java] Personal summary of classes and methods (basic)
About Java data types (especially primitive types) and literals
Java upload and download notes to Azure storage
Upload and download notes in java on S3
[Java] Stream Collectors notes
Java formatted output [Notes]
Java instruction execution statement
Getters and setters (Java)
[Java] Types of comments and how to write them
[Java] Thread and Runnable
Java true and false
java basic knowledge memo
[Java] String comparison and && and ||
[Java] Data type ①-Basic type
[Java] Control syntax notes
credentials.yml.enc and master.key <Notes>
Java NIO 2 review notes
Java basic date manipulation
Java review ① (development steps, basic grammar, variables, data types)
Java basic naming conventions
Java learning memo (basic)
Java --Serialization and Deserialization
[Java] Arguments and parameters
I summarized the types and basics of Java exceptions
timedatectl and Java TimeZone
Maven basic study notes
[Java] Branch and repeat
[Java] Main data types