0% found this document useful (0 votes)
22 views20 pages

Software Project Lecture 2

The document summarizes key concepts about the Java programming language. It discusses that Java was first released in 1995 and is now licensed by Oracle. It then describes main features like platform independence, object-oriented programming, automatic memory management, and abundant libraries. It also explains the Java development kit, byte code, the Java virtual machine, package structure, variables, primitive data types, and Boolean logic.

Uploaded by

박진영
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views20 pages

Software Project Lecture 2

The document summarizes key concepts about the Java programming language. It discusses that Java was first released in 1995 and is now licensed by Oracle. It then describes main features like platform independence, object-oriented programming, automatic memory management, and abundant libraries. It also explains the Java development kit, byte code, the Java virtual machine, package structure, variables, primitive data types, and Boolean logic.

Uploaded by

박진영
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Software Project Spring 2023

Software Project (Java Programming)

Lecture 2. Basics of Java

Joon-Woo Lee

School of Computer Science and Engineering


College of Software
Chung-Ang University
Software Project Spring 2023

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

Main Features of Java Language


• Runs on any OS
• Programs written in Java run on any operating system
• A program developed in Windows can run on macOS or Linux without any
modifications.
• Object-oriented programming (OOP)
• OOP is the technique of creating objects (parts) and then connecting them
together to form a larger program.
• Automatic memory management
• Java automatically manages RAM so that developers don't have to worry about
memory management and can focus on writing core functionality.
• Abundance of free libraries
• The abundance of open source libraries reduces program development time.

3
Software Project Spring 2023

Java Development Kit (JDK)


• JDK is the implementation of Java Standard Edition (Java SE).
• To develop and run a Java program, you should first install the JDK.
• There are two types of JDKs: Oracle JDK and Open JDK.
• Oracle JDK
• Free for education and development, Non-free for industry
• Better performance in JVM
• Open JDK
• Free!
• Performance is constantly improving
• We will use Open JDK in this class.
4
Software Project Spring 2023

Byte Code and Java Virtual Machine

• Java source file (text file): .java file


• byte code: .class file
• compilation to byte code: javac command
• Compiling the same source file with javac on any operating system.

Java source file Byte code file


(Hello.java) (Hello.class)

javac command
(compilation)

5
Software Project Spring 2023

Byte Code and Java Virtual Machine


• Java Virtual Machine (JVM): bridge software between the Java program
and the operating system.
• java command: translating bytecode files into machine language and
executing.
• The java command runs the Java virtual machine installed with the JDK to translate
and execute the bytecode file into full machine language.
• JVM is installed differently on each operating system.
• It must be translated into machine language understood by the operating system.

Byte code file Machine language


00110110010101010 Execution
(Hello.class) 10001010101100101

java command
(running JVM)
6
Software Project Spring 2023

source file
(*.java)
javac
command
byte code file
(*.class)

JVM for windows java JVM for macOS


command
translation into translation into
machine code for machine code for
windows and execution macOS and execution

Windows OS macOS

Hardware Hardware

7
Software Project Spring 2023

Package Structure and Source Code


• Package declaration: This refers to the location of the source files and
byte code files.
• source code: src/ch01/sec09
• byte code: bin/ch01/sec09
• Class declarations: It must match the source filename. (Hello.java)
• main method: program entry point
package ch01.sec09;

public class Hello {


public static void main(String[] args) {
System.out.println(“Hello, Java”);
}
}
8
Software Project Spring 2023

Variable Declaration and Initialization


• Variable declaration and initialization is the same as C language.
• Using an uninitialized variable in an expression results in a compilation error.
• A variable can be combined with a string and then output or utilized in an
expression.
package ch02.sec01;

public class VariableInitializationExample {


public static void main(String[] args) {
int value;
int result = value + 10; // compilation error!
System.out.println(result);

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

Primitive Type in Java


• Java provides 8 primitive types for storing integers, floating-point numbers,
and boolean values.
• Integer literals are regarded as ‘int’ type, if we use literals for ‘long’ type, it
must be attached with l or L.
• Floating-point literals are regarded as ‘double’ type, if we use literals for
‘float’ type, it must be attached with f or F.
• The empty literal for ‘char’ type is one space ‘ ', not a real empty character
‘’.
Value Type Primitive Type (byte)
Integer byte(1), char(2), short(2), int(4), long(8)
Floating-point float(4), double(8)
Boolean (true/false) boolean

10
Software Project Spring 2023

Boolean Type
package ch02.sec05;

public class BooleanExample {


• The logical literals are true and false. public static void main(String[] args) {
boolean stop = true;
• Logical literals can be assigned to if(stop) {
System.out.println(“abort!”);
variables of 'boolean‘ type. } else {
System.out.println(“start!”);
• The outputs of comparison and }

logical operations are true or false, so int x = 10;


they can be assigned to ‘Boolean’ boolean result1 = (x == 20);
boolean result2 = (x != 20);
type variables. System.out.println(“result1: “ + result1);
System.out.println(“result2: “ + result2) ;
}
}

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”

is stored as a string as it is written. “””;


String var4 = “””
• Starting with Java 14, the text block I’m learning \
Java.
syntax allows you to continue writing I will be a Java master.
to the next line without a newline, by “””;

placing a backslash (\) at the end.

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

Type with Type with Type with


smaller range
= ( smaller range
) larger range
• Shrinking a type with larger range
into a type with smaller range is
called typecasting. int intValue = 10;
byte byteValue = (byte) intValue;
• Typecasting uses parentheses() as an
long longValue = 300;
operator. int intValue = (int) longValue;

• Make sure that the value can be int intValue = 65;


stored in the type with smaller range char charValue = (char) intValue;
System.out.println(charValue); // ‘A’ is printed.
before performing the typecasting.
double doubleValue = 3.14;
int intValue = (int) doubleValue; // 3 is stored.

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!

variable is used in an integer byte x = 10;


byte y = 20;
operation, it is better for execution byte ByteResult = x + y; // compile error!
performance to declare an int or long int IntResult = x + y; // OK!

type variable.
15
Software Project Spring 2023

Two functions of '+‘ operation


• If all operands are numbers, the
addition operation is performed for ‘+’
operator. int value = 3 + 7; // 10
String str = “3” + 7; // “37”
• If one of two operands is a string, the String str = 3 + “7” // “37”

concatenation operation is performed int value = 1 + 2 + 3; // 6


for ‘+’ operator. String str = 1 + 2 + “3” // “33”
String str = 1 + “2” + 3; // “123”
• When several + operators occurs in String str = “1” + 2 + 3; // “123“

an expression, the + operation is String str = “1” + (2 + 3) // “15”


performed sequentially, starting at
the beginning.

16
Software Project Spring 2023

Conversion of String and Primitive Types


Conversion Type Examples
String  byte String str = “10“;
byte value = Byte.parseByte(str);
String  short String str = “10“;
short value = Short.parseShort(str);
String  int String str = “10“;
int value = Integer.parseInt(str);
String  long String str = “10“;
long value = Long.parseLong(str);
String  float String str = “10“;
float value = Float.parseFloat(str);
String  double String str = “10“;
double value = Double.parseDouble(str);
String  boolean String str = “10“;
boolean value = Boolean.parseBoolean(str);
primitive  String int value = 100;
String str = String.valueOf(value);
17
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;

public class ScannerExample {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(“write x value: “);
String strX = scanner.nextLine();
int x = Integer.parseInt(strX);

System.out.print(“write y value: “);


String strY = scanner.nextLine();
int y = Integer.parseInt(strX);

int result = x + y;
System.out.println(“x + y: “ + result);
}
}

20

You might also like