How to use Java variables

This article was written for people who were programming in other languages and would like to program in Java. This time, I will explain about variables.

What is a variable?

A variable is an area that stores data. Roughly speaking, it's like a box where you can put values.

Variable type

Listed for each variable type.

Numerical system

Data type name name Memory consumption range
byte Byte type 1 byte -128~127
short Short integer type 2 bytes -32768~32767
int Integer type 4 bytes -2147483648~2147483647
long Long integer type 8 bytes -9223372036854775808~ 9223372036854775807
float Single precision floating point type 4 bytes ±3.40282347E+38~ ±1.40239846E-45
double Double precision floating point type 8 bytes ±1.79769313486231570E+308~ ±4.94065645841246544E-324

Character system

Data type name name Memory consumption range
char Character type 2 bytes ¥u0000~¥uFFFF
String String type 4 bytes(8 bytes for 64-bit JVM) Reference type

Special type

Data type name name Memory consumption range
boolean Boolean type 1 byte true(true)/false(false)

Variable declaration

Variables require a variable name and a data type name. The format is "Data type name (half-width space) variable name;". Example)

example.java


int i;
String s;

Variable initialization

If you just declare it, only the value determined for each type will be entered. You need to enter the value you want to enter. The format is "variable name = desired value;". Initialization can be performed at the same time as the declaration. In that case, write "Data type name (half-width space) variable name = value you want to enter;".

example.java


int i;
String s;

i = 1;
s = "str string";
char c = 'Ah';

However, please be careful because the value that can be entered is fixed for each type. Also, you have to declare it first. bad example)

bad_example.java


int i;
String s;

i = "str string"; //String type to integer type
s = 2432; //String type to number type
c = 'Ah'; //No declaration

How to convert (cast)

This is a summary of the contents of [Introduction to Java] Cast (type conversion), upcast, and downcast. Type conversion is the conversion of one type to another. Some are declared to be automatic and some are declared. First of all, the example below is automatic and easy to understand.

example.java


int i = 2;
float f = i;

System.out.println("int i = " + i);
System.out.println("float f = " + f);

System.out.println (); prints the characters in (). When you run this program int i = 2 float f = 2.0 Is output. In this case, i and f are treated as string types. In other words, it has been converted to a String type. Also, int type i can be assigned to float type f. This is because the value of i is automatically changed to float type. The current example is a type conversion (cast) that is performed automatically.

Next, if you do it yourself. Here's an example of what it's like to do it yourself.

example.java


String s1 = "9";
String s2 = "1";

System.out.println(s1 + s2);

When you run this program 91 Is output. Because it is a String type, the character string "9" and the character string "1" are combined into one character string. However, if you want to calculate and get 10 here, write as follows.

example.java


String s1 = "9";
String s2 = "1";

System.out.println(Integer.parseInt(s1) + Integer.parseInt(s2));

Integer.parseInt (); is a process to change the character string in () to int type. This makes it possible to calculate the values of s1 and s2 as int type, so the output result is 10 It will be.

How to convert

Numerical types

In the case of number type, (type name) value can be exchanged.

number.java


int i = 1;
short s = (short)i;
float f = 2.4;
long l = (i+s+f);
System.out.println(l); //Output result = 5

Number → character

Use String.valueOf () ;. Or connect it with a character string with +.

number.java


int i = 3;

System.out.println(String.valueOf(i)+2); //Output result = 32
System.out.println("number" + i); //出力結果=number3

Letters → numbers

Except for int type [Data type name (first letter only uppercase)] .parse Data type name (first letter only uppercase); int type Described as Integer.parseInt (character) ;.

number.java


String s = 03;

int i = Integer.parseInt(s);
int l = Long.parseLong(s);

System.out.println(i); //Output result = 3
System.out.println(l); //Output result = 3

Precautions for type conversion

There are the following points to note about type conversion. -Since type conversion only specifies the type of the received value, the value of the original variable is not changed even if the variable name is entered. -If the received value does not match the person you want to convert, an error will occur. -As a result of conversion, the value may change before and after conversion.

Example)

bad_example.java


String s1 = "sss9";
String s2 = "1";
String s3 = "03";

System.out.println(Integer.parseInt(s1) + Integer.parsparseInt(s2); //"sss"Error because the part is not an integer
System.out.println(Integer.parseInt(s3)); //Is output as 3. 0 disappears
System.out.println(s3); //03 is output. Integer on the top line.parseInt(s3)Is not affected by.

Recommended Posts

How to use Java variables
[Processing × Java] How to use variables
[Java] How to use Map
[Java] How to use Map
How to use java Optional
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
[Java] How to use Optional ①
How to use Java HttpClient (Get)
How to use Java HttpClient (Post)
[Java] How to use LinkedHashMap class
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
How to name variables in Java
[Processing × Java] How to use arrays
How to use Java lambda expressions
[Java] How to use Math class
How to use Java enum type
How to use Map
[Java] How to use the File class
How to use rbenv
[Java] How to use the hasNext function
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use submit method (Java Silver)
[Java] How to use the HashMap class
How to use collection_select
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use Java classes, definitions, import
[Easy-to-understand explanation! ] How to use Java polymorphism
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class
How to use Java Scanner class (Note)
How to use JUnit 5
[Processing × Java] How to use the function
How to use environment variables in RubyOnRails
[Easy-to-understand explanation! ] How to use ArrayList [Java]
How to use Dozer.mapper
[Java] How to use the Calendar class
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
[Java] Learn how to use Optional correctly
[Easy-to-understand explanation! ] How to use Java overload
How to use Map