Software Project Lecture 2
Software Project Lecture 2
Joon-Woo Lee
Java Language
• First released by Sun Microsystems in 1995.
• One of the most successful programming languages.
• Licensed by Oracle.
• Oracle distributes Java development kit (JDK) and provides technical
support to develop programs with Java.
2
Software Project Spring 2023
3
Software Project Spring 2023
javac command
(compilation)
5
Software Project Spring 2023
java command
(running JVM)
6
Software Project Spring 2023
source file
(*.java)
javac
command
byte code file
(*.class)
Windows OS macOS
Hardware Hardware
7
Software Project Spring 2023
int hour = 3;
int minute = 5;
int totalMinute = (hour * 60) + minute;
System.out.println(“It takes “ + hour + “ hours “ + minute + “ minutes.");
System.out.println(“Total minute: “ + totalMinute) ;
}
}
9
Software Project Spring 2023
10
Software Project Spring 2023
Boolean Type
package ch02.sec05;
11
Software Project Spring 2023
String Type
• If you want to store a string in a
variable, you should use the 'String' String var = “Apple”;
type. String var2 = “I\’m learning \”Java\”.\n”;
String var3 = “””
• You can also use escape characters. {
“id”:”winter”,
• Java 13 provides a text block syntax. It }
“name”:”snow”
12
Software Project Spring 2023
Promotion
promotion
• A promotion is an automatic type
conversion that occurs when a type Type with
=
Type with
larger range smaller range
with a smaller allowed range is
substituted for a type with a larger
allowed range. byte byteValue = 10;
int intValue = byteValue;
• The primitive types is listed in order
of allowed range as follows. long longValue = 5000000000L;
float floatValue = longValue;
• byte < short, char < int < long < float double doubleValue = longValue;
< double.
char charValue = 'A';
• A 'byte' type cannot be promoted to int intValue = charValue;
a char type.
byte byteValue = 65;
• char type does not include negative char charValue = byteValue; // compile error!
numbers, while the byte type does.
13
Software Project Spring 2023
Type Casting
type casting
14
Software Project Spring 2023
Promotion in Operation
• A byte, short, char type variable is byte byte
automatically converted to an int char operator char
int result =
type to perform the operation. short (+,-,*,/,%) short
int int
• If you try to store the result of an
operation on 'byte' variables in a int int
'byte' variable, a compilation error
occurs.
• Unless there is a special reason, if a byte result = 10 + 20; // OK!
type variable.
15
Software Project Spring 2023
16
Software Project Spring 2023
Printing
• We can print strings on console windows by this commands.
• System.out.println/System.out.print/System.out.printf
• println: Print the contents in the parentheses and have a new line.
• print: Print the contents in the parentheses and do not have a new line.
• printf: Print the contents in the parentheses with some formats.
format explanation print
integer %d integer 123
%6d integer with 6 digit. empty space in the left. ___123
%-6d integer with 6 digit. empty space in the right. 123___
%06d integer with 6 digit. 0's in the left. 000123
floating-point %10.2f 7 digit integer+decimal point+2 digit fraction. empty in the left ____123.45
%-10.2f 7 digit integer+decimal point+2 digit fraction. empty in the right 123.45____
%010.2f 7 digit integer+decimal point+2 digit fraction. 0’s in the left 0000123.45
string %s string abc
%6s string with 6 characters. empty space in the left ___abc
%-6s string with 6 characters. empty space in the right abc___
special character \t tab
\n newline
%% % %
18
Software Project Spring 2023
Scanning
• To read data from the keyboard and store the data, we can use
Scanner type.
• We should import java.util.Scanner on top of the source code.
• Declare a variable of type Scanner, and use the new operator as
follows.
• Scanner scanner = new Scanner(System.in);
• Then we run scanner.nextLine() to read the keyboard input as a string
and store it in the left String variable.
• String inputData = scanner.nextLine();
• scanner.nextLine() will wait for the enter key to be pressed, and when
it is, it will read everything that has been typed so far as a string.
19
Software Project Spring 2023
Scanning
package ch02.sec13;
import java.util.Scanner;
int result = x + y;
System.out.println(“x + y: “ + result);
}
}
20