[JAVA] printf / arguments / variables / types

printf

printf can pass arguments
What is it possible to pass arguments?
You can freely enter the set characters in the standard format you created. Is it worth the template literal in JavaScript? ?? ..
-For Java printf int num = 5; int num2 = 10; System.out.printf("%d + %d = %d", num, num2, num + num2).println();

↓ 5+10=15

Variables and types Variables are like boxes that temporarily store data. You can make changes as many times as you like. The type and capacity (number of characters) of the data that can be stored will change depending on the type of variable specified. Also, the variable name can be decided freely, but there are some rules.


Variable type type
byte: Shorter number (up to about 2 digits) short: up to 4 digits int: Numbers up to 9 digits long: Large number of digits such as world population float: Floating point. I'm not sure, but I don't really recommend it double: Floating point. This one seems to be common. The number of digits that can be handled is larger than float. char: Only one character String: String boolean: Boolean value I can't really imagine where to use it.


Variable name rules
-Alphanumeric characters can be used for variable names, but underscores [_] and other symbols other than dollar marks [$] cannot be used. -Reserved words used in Java cannot be used. ・ You cannot start with numbers. ・ There is no limit to the number of characters -Case sensitive. -It is common to use camel case (otherwise Java will not read it) ex) like telephonNumber, firstName, populationOfWorld The first letter of the first English word, which has two or more words, is written in lowercase letters, and the first letter of the words after that is written in uppercase letters.

Code executed today jshell> int x = 4 x ==> 4

jshell> int y = 10 y ==> 10

jshell> int z = 5 z ==> 5

jshell> System.out.printf("%d + %d + %d = %d", x, y, z, x + y + z).println() 4 + 10 + 5 = 19

jshell> x = 7 x ==> 7

jshell> System.out.printf("%d + %d + %d = %d", x, y, z, x + y + z).println() 7 + 10 + 5 = 22

jshell> y = 66 y ==> 66

jshell> System.out.printf("%d + %d + %d = %d", x, y, z, x + y + z).println() 7 + 66 + 5 = 78

jshell> z = 43 z ==> 43

jshell> System.out.printf("%d + %d + %d = %d", x, y, z, x + y + z).println() 7 + 66 + 43 = 116

jshell> Double d = 3.4 d ==> 3.4

Recommended Posts

printf / arguments / variables / types
[Java] Variables and types
Java Primer Series (Variables and Types)
Java array variables are reference types
Java starting from beginner, variables and types