There are two data types in Java : Primitive and Reference.
They are the basic of basic in Java and very important to know to jump to the next step, so let's make them familiar!
Primitive Type
Type | Size | Description |
byte | 1 byte | Stores whole numbers from -128 to 127 |
short | 2 bytes | Stores whole numbers from -32,768 to 32,767 |
int | 4 bytes | Stores whole numbers from -2,147,483,648 to 2,147,483,647 |
long | 8 bytes | Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float | 4 bytes | Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits |
double | 8 bytes | Stores fractional numbers. Sufficient for storing 15 decimal digits |
boolean | 1 bit | Stores true or false values |
char | 2 bytes | Stores a single character/letter or ASCII values |
Reference Type
Type | Description |
Class | Describes the content of the object |
Array | Stores the elements of the same type |
Interface | A collection of abstract methods |
The difference between Primitive and Reference Type is that
Variables
Variable is a storage space for storing data on memor.
package p2022_06_21;
import java.util.ArrayList;
public class Variable {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1. Integer Type
byte b1 = 10;
short s = 100;
int i = 1000;
long l = 100000L;
System.out.println("b1="+b1);
System.out.println("s="+s);
System.out.println("i="+i);
System.out.println("l="+l);
//2. Rational Type
float ft1 = 3.14f;
// float ft1 = (float)3.14;
float ft2 = 3.14F;
double d = 42.195;
System.out.println("ft1="+ft1);
System.out.println("ft2="+ft2);
System.out.println("d="+d);
System.out.printf("%.1f\n", d);
System.out.printf("%.2f", d);
System.out.printf("%.2f\n",d);
//3. Character Type
char c1 = 'A';
char c2 = '安';
System.out.println("c1="+c1);
System.out.println("c2="+c2);
//4. Boolean Type
boolean bn1 = true;
boolean bn2 = false;
System.out.println("bn1="+bn1);
System.out.println("bn2="+bn2);
//Reference Type : Class
String s1 = "Java";
String s2 = new String("Java");
System.out.println("s1="+s1);
System.out.println("s2="+s2);
if(s1 == s2){
System.out.println("Same Address");
} else {
System.out.println("Different Address");
}
if(s1.equals(s2)){
System.out.println("Same Value");
} else {
System.out.println("Different Value");
}
//Reference variable : 배열- 동일한 자료형의 데이터를 저장하는 정적인 자료구조
int[] score = {80, 90, 100};
for(int j=0; j<score.length; j++) {
System.out.println(score[j]+"\t");
}
System.out.println();
//Reference Type : Interface(List)
//List list = new List(); //Error
ArrayList list = new ArrayList(); //Upcasting
list.add(30);
list.add(3.14);
list.add('j');
list.add(true);
list.add("Java");
for(int k=0; k<list.size(); k++) {
System.out.print(list.get(k)+"\t"); //\t는 insert a new tap
}
}
}
'Java' 카테고리의 다른 글
Java) Array (0) | 2022.09.04 |
---|---|
Java) .length vs length() (0) | 2022.09.02 |
Java) Conditional statements and loops (0) | 2022.08.30 |
Java) Operators (0) | 2022.08.26 |
Java (Must know) (0) | 2022.08.25 |