Day 2) Starting Coding and Hello World with Eclipse _2018-05-29 am
[Morning Class — Java Basics]
#Starting coding
1. Prepare either notepad or notepad++.7.3.Installer — whichever is closer.
Create a new file
public class Hello{
public static void main(String[] arg){
System.out.println("Hello Java");
}
}
2. Save to the desktop
Hello.java
3. Open cmd at the desktop location
javac Hello.java // run the javac command to convert the code into something the JVM can understand = compile
-> 'Hello.class' is created on the desktop
4. Run
java Hello
-> Hello Java
# Repeating the compile process manually as above is a lot of trouble,
so we use Eclipse. (Provides conveniences such as auto-compile on save.)
http://www.eclipse.org
http://www.eclipse.org/ide
http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/oxygen3a
#Starting Eclipse
0. Setup
1) JDK setup
- 1.7
- 1.8
2) windows > preferences
- encoding
- font size adjustment
3) Set perspective to Java
4) Create a new project 'basicJava' (Ctrl+n)
> Create an empty package 'a_variable' under the empty src (Ctrl+n) > Create individual classes (Ctrl+n)
1. Create the Hellojava class
package a_variable;
public class Hello {
public static void main(String[] args){
System.out.println("Hello Java");
}
}
2. Create the VariableBasic class
package a_variable;
/**
* doc comment
* Learning the rules and conventions for declaring variables
* @author bcs
* @since 20180529
**/
// single-line comment 1
// single-line comment 2
// ctrl + shift + c
/*
* multi-line comment
*/
/*
1. What is a variable — a space that can store exactly one value
2x + 3y = 10 ;
int x = 2 ; // writing the family and given name is what counts as a declaration.
// a "family" (area) that can hold integer values
// int = variable type
int y = 2 ;
x = 7 // because only one value can be stored, the end result is
// x = 7 ;
2. Variable declaration
int x ;
variable type variable name
3. Rules for declaring variable names, method names, and class names
1) Case sensitive, but length of the word is not restricted
2) Reserved words (keywords) pre-declared by Java — shown in orange —
cannot be used.
3) Cannot start with a number
4) For special characters, only _ and $ are allowed.
4. Conventions among Java developers
1) Class names start with an uppercase letter
- Method and variable names start with a lowercase letter
- Method names are followed by ( )
2) For multi-word names, capitalize the first letter of each new word
3) When declaring a variable as a constant (e.g. MAX_VALUE), use all uppercase
and join multiple words with _
4) Do not use Korean
*/
public class VariableBasic{
// class name
public static void main(String[] arg){
// method name // (method)
int x ; // variable declaration
x = 10 ; // variable initialization
int y = 10 ; // declaration and initialization
x = 60 ;
final int xyz = 20 ; // constant — always the same value.
// note: the variable name should be all uppercase (-> XYZ)
z = 40 ; // error — we tried to assign to a constant
}
}
3. Create the VariableOther class
package a_variable;
/**
* Variable types and number-base arithmetic
* @author bcs
* @since 20180529
**/
public class VariableOther{
public static void main(String[] args){
/*
Computers understand data in binary, Java in hexadecimal form
Data sizes
1 byte = 8 bits = 8 slots = the smallest unit of storage
1 bit = 1 slot = can be expressed as 0 or 1 = binary
1 bit = min 0, max 2
2 bit = min 0, max 3
4 bit = min 0, max 15
8 bit = min 0, max 255
Primitive Type -> store data as a value
boolean, char, byte, short, int, long, float, double
1) By kind — primitive types
- Boolean: stores one of true/false > boolean
- Character: stores a single character > char
- Integer: stores a single whole number (-, 0, +) > byte short int long
- Floating-point: stores a real number (with decimals) > float double
2) By size — primitive types
- 1 byte > boolean, byte
- 2 byte > char, short
- 4 byte > int, long
- 8 byte > float, double
3) Table form
|
1byte |
2byte |
4byte |
8byte |
Boolean | boolean |
|
|
|
Character |
| char |
|
|
Integer | byte | short | int | long |
Floating-point |
|
| float | double |
Reference Type -> store values in the form of addresses
Base-conversion examples
10 -> binary
1010(2)
65 -> hexadecimal
41(16)
204 -> hexadecimal
12,12 = CC(16)
// hex 0~15 => 0123456789ABCDEF
1101(2) -> decimal
13
1f(16) -> decimal
31
}
}
