--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 -
boolean:
trueor
false`
char
: 1 character
--String
--String
: StringFollows 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);
}
}
--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).");
}
}
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 ↑.
InputStreamReader
BufferedReader
--Method:readLine()
In addition, it seems that no error will occur if you hit javac and java directly at the command prompt.
[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