Java
Variables
WebDevLee
2022. 1. 31. 12:06
자바의 변수에 대한 기본 개념 및 문법을 정리하였습니다.
< Introduction >
To store information, use variables, named location in memory.
Storing a information allows us to use that information later, accessing the information we stored.
- When declare variable, specify the type of information we're storing. There's two main data-types.
1. Primitive Type
2. Non-Primitive Data Types(called Reference Type)
< Primitive Data Type >
Data-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 |
Example)
int year = 2000;
char myName = 'Lee';
boolean olderThanTwenty = true;
Reference : Java Data Types
Java Data Types
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
www.w3schools.com
< int >
To store Whole Number.
Example)
// int variable declaration
int yearJavaWasCreated;
// assignment
yearJavaWasCreated = 1996;
// declaration and assignment
int numberOfPrimitiveTypes = 8;
< doubles >
To store Decimal.
Example)
// doubles can have decimal places:
double price = 8.99;
// doubles can have values bigger than what an int could hold:
double gdp = 12237700000;
< booleans >
Two values : true or false (like yes or no)
Example)
boolean javaIsACompiledLanguage = true;
boolean javaIsACupOfCoffee = false;
< char >
Hold any character, like a letter, space, or punctuation mark.
It must be surrounded by single quotes, ' ' not " "
Example)
char grade = 'A';
char firstLetter = 'p';
char punctuation = '!';
< String >
String is an object, have built-in behavior (Reference Type)
String holds sequences of characters.
It must be surrounded by double quotes, " " not ' '
Example)
String greeting = "Hello World";
String salutations = new String("Hello World");
: Also create a new String object by calling the String class when declaring a String.
+. Escape Character : \
- To remove the meaning of the character that follows.
Example)
System.out.println("\"Hello World\"");
// "Hello World"
System.out.println("This is the backslash symbol: \\");
// This is the backslash symbol: \
System.out.println("Hello\nGoodbye");
// Hello
// Goodbye
: \n means Spacing
< Naming >
Naming variables according to convention leads to clear, readable, and maintainable code.
- In Java, variable names are case-sensitive.
myvariable is a different variable from myVariable - A variable starts with a valid letter, or a $, or a _ , No other symbols or numbers can begin a variable name.
1stPlace and *Gazer are not valid variable names. - Variable naming in Java follows camelCase style
: Variable names of more than one word have the first letter lowercase while the beginning letter of each subsequent word is capitalized.
Example)
// bad naming
String data = "Delilah";
// good naming
String nameOfUser = "Delilah";
// good style
boolean isHuman;
// bad styles
// no capitalization for new word
boolean ishuman;
// first word should be lowercase
boolean IsHuman;
// underscores don't separate words
boolean is_human;