Download presentation
Presentation is loading. Please wait.
1
Introduction to Computing Using Java
Java Language Basics a Michael Fung, CS&E, The Chinese University of HK
2
Common Syntax/ Grammar Notations
Arrow-bracketed item <someName> or <someName> Fill in this blank WITHOUT the arrows This is a place-holder DO NOT type the arrows in actual code! Brackets and punctuations { } ( ) [ ] , ; . Type carefully and place in appropriate location a Michael Fung, CS&E, The Chinese University of HK
3
Michael Fung, CS&E, The Chinese University of HK
Quotes and Brackets ' Single Quote 單弔引號 " Double Quote 雙引號 ( ) Brackets/ Parentheses 括號/ 小括號 [ ] Square Brackets 方括號/ 中括號 { } Curly Brackets/ Braces 花括號/ 大括號 ; Semi-colon 分號 . Dot/ Full-stop 點 , Comma 逗號 a Michael Fung, CS&E, The Chinese University of HK
4
Java Naming Convention (Capitalization)
Examples keyword class, if, double, int, for, while packagename javax.swing, javaapplication12 ClassName String, JOptionPane, Double FileName HelloWorld.java, JOptionPane.class methodName() showMessageDialog( ), parseInt( ) fieldName System.out, DrinkDispenser.cokeStock variableName numberOfStudents, bodyWeight CONSTANT JFrame.HEIGHT, Math.PI a Michael Fung, CS&E, The Chinese University of HK
5
Java Program Structure
import <packagename>.<someclassname>; import <packagename>.*; class <ClassName> { <type> <fieldName1>; <type> <fieldName2>; <type> <methodName1> ( ... ) } <type> <methodName2> ( ... ) Field (Data Member) Declarations Method Declarations a Michael Fung, CS&E, The Chinese University of HK
6
Michael Fung, CS&E, The Chinese University of HK
Java Program Example import java.util.Vector; import javax.swing.*; class CurrencyConverter { double rateHKtoEuro = ; double rateHKtoUS = 0.128; double convertEuro2US ( double amountEuro ) { // details skipped } double convertUS2HK ( double amountUS ) Field (Data Member) Declarations Method Declarations a Michael Fung, CS&E, The Chinese University of HK
7
Michael Fung, CS&E, The Chinese University of HK
Data Item Declaration Data items are used to store data. Such as fields, local variables, constants, … We give name (an identifier) to each data item. e.g. “Ma Liu Shui, Tai Po Road, Shatin.” is a piece of data. This information is called CUHKaddress. Each data item must bear a type. CUHKaddress is of type String (text). a Michael Fung, CS&E, The Chinese University of HK
8
Michael Fung, CS&E, The Chinese University of HK
Identifier Names introduced by programmers in computer languages are called identifiers. e.g. Address hello_World One2Free j_u_s_t___2___say___hi Ring a Michael Fung, CS&E, The Chinese University of HK
9
Michael Fung, CS&E, The Chinese University of HK
Java Identifiers Valid components ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz _ “underscore” Space is disallowed. Must start with a letter or underscore. e.g. 12Free is disallowed while One2Free is okay Case sensitive. e.g. Hello is not equal to hello nor HellO a Michael Fung, CS&E, The Chinese University of HK
10
Michael Fung, CS&E, The Chinese University of HK
Choice of Identifiers Easy to understand and remember. e.g. noOfEnquiries, Trees, timeToLive, name, address, count, isOK, List, experiment12 May use multiple words (without space!). e.g. grade_point_average, myBirthday, numberOfStudents Don’t be lazy! e.g. i, j, k, a, b, c, d, e, … a Michael Fung, CS&E, The Chinese University of HK
11
Michael Fung, CS&E, The Chinese University of HK
What Kind Is It? Given you an identifier noOfStudents in my Java program, do you (most importantly, the computer or the compiler) know what it is? It may be: a class name a method name a field name (could be variable or constant) a local variable name (data item declared in a method) a Michael Fung, CS&E, The Chinese University of HK
12
Michael Fung, CS&E, The Chinese University of HK
What Kind Is It? It all depends on where we put it in our program. Let’s now consider the third case, field name. When an identifier appears in a data item declaration within a class BUT not in a method, it is a field name. e.g. class XYZ { int noOfStudents; JFrame frame; double GPA, termGPA; } a Michael Fung, CS&E, The Chinese University of HK
13
Michael Fung, CS&E, The Chinese University of HK
What Kind Is It? It all depends on where we put it in our program. When an identifier appears in a data item declaration within a method, it is a local variable. e.g. class XYZ { public static void main(String[] args) { int noOfStudents; JFrame frame; double GPA, termGPA; } a Michael Fung, CS&E, The Chinese University of HK
14
Declaration of a Field/ Local Variable
“Type” refers to both primitive type or class. In general, four basic forms of declarations: <type name> identifier; <type name> identifier1, identifier2, … ; <type name> identifier = <initial value>; <type name> identifier1 = <initial value1>, identifier2 = <initial value2>, …; Examples of the last two forms: String myName = "Michael Fung"; double HangSengIndex = , rainfall = 3.4; a Michael Fung, CS&E, The Chinese University of HK
15
Michael Fung, CS&E, The Chinese University of HK
What is Type? A type restricts the kind and range of values a data item or an expression could take. e.g. Sex [‘M’, ‘F’] HKID K123456(7) Type must be matched (各從其類) e.g. String name = "Chan Tai Man"; String myName = 123; // 123 is a number, neither text nor String // "123" however, is a String a Michael Fung, CS&E, The Chinese University of HK
16
What are Primitive Types?
Eight build-in (primitive) types in Java: byte for storing a 8-bit signed integer short for storing a 16-bit signed integer int for storing a 32-bit signed integer long for storing a 64-bit signed integer float for storing a 32-bit real number double for storing a 64-bit real number char for storing a single character boolean for storing a true/false (logic) value a Michael Fung, CS&E, The Chinese University of HK
17
What are Primitive Types?
Eight build-in (primitive) types in Java: byte [-128 127] short [ 32767] int [ ] long [ ] float [±1038 with floating point] double [±10308 with floating point] char [‘A’, ‘B’, …, ‘a’, ‘b’, …, ‘0’, ‘1’, …, ‘!’, ‘#’] boolean [true, false] a Michael Fung, CS&E, The Chinese University of HK
18
Michael Fung, CS&E, The Chinese University of HK
Use of Primitive Types To store a number double pi = ; To store an age of 108 byte age = 108; To find and keep the reciprocal of 1997 double answer = 1.0 / 1997; a Michael Fung, CS&E, The Chinese University of HK
19
Assignment Statement/ Expression
To store a value into a data storage on the left-hand-side. e.g. int temperature, oldWeight, newWeight; double GPA; temperature = 24; oldWeight = 110; newWeight = oldWeight – 5; GPA = 4.0; GPA = 3; a Michael Fung, CS&E, The Chinese University of HK
20
Assignment Statement/ Expression
A field or local variable ‘takes the value of’ (‘becomes’) something. The effect is instantaneous and lasting onwards, but not back-dated. Type MUST be matched. e.g. birds and elephants are different Implicit coercion (type conversion) may be done by the compiler. e.g. for numeric types (see example later) a Michael Fung, CS&E, The Chinese University of HK
21
Michael Fung, CS&E, The Chinese University of HK
Expression Examples: (-5) (1.234 / 56 + Pi) * 9.8 m * c * c USdollar * 7.80 Use with assignment operator: HKdollar = USdollar * 7.80; a Michael Fung, CS&E, The Chinese University of HK
22
Michael Fung, CS&E, The Chinese University of HK
7-Minute Break a Michael Fung, CS&E, The Chinese University of HK
23
Michael Fung, CS&E, The Chinese University of HK
Thomas Cook class UStoHKConverter { static final double rate = 7.80; public static void main (String [ ] args) { double USdollar = ; double HKdollar = USdollar * rate; System.out.println( "US" + USdollar + " -> HK" + HKdollar); } rate 7.80 a Michael Fung, CS&E, The Chinese University of HK
24
Thomas Cook Modifiers/ Adjectives to be discussed later
class UStoHKConverter { static final double rate = 7.80; public static void main (String [ ] args) { double USdollar = ; double HKdollar = USdollar * rate; System.out.println( "US" + USdollar + " -> HK" + HKdollar); } rate 7.80 Modifiers/ Adjectives to be discussed later A class (static) constant (final) a Michael Fung, CS&E, The Chinese University of HK
25
Thomas Cook A field declaration with initialization and some modifiers
class UStoHKConverter { static final double rate = 7.80; public static void main (String [ ] args) { double USdollar = ; double HKdollar = USdollar * rate; System.out.println( "US" + USdollar + " -> HK" + HKdollar); } rate 7.80 A field declaration with initialization and some modifiers a Michael Fung, CS&E, The Chinese University of HK
26
Thomas Cook A local variable declaration with initialization
class UStoHKConverter { static final double rate = 7.80; public static void main (String [ ] args) { double USdollar = ; double HKdollar = USdollar * rate; System.out.println( "US" + USdollar + " -> HK" + HKdollar); } rate 7.80 A local variable declaration with initialization a Michael Fung, CS&E, The Chinese University of HK
27
Michael Fung, CS&E, The Chinese University of HK
Thomas Cook class UStoHKConverter { static final double rate = 7.80; public static void main (String [ ] args) { double USdollar = ; double HKdollar = USdollar * rate; System.out.println( "US" + USdollar + " -> HK" + HKdollar); } rate 7.80 An expression a Michael Fung, CS&E, The Chinese University of HK
28
Thomas Cook Another local variable declaration with initialization
class UStoHKConverter { static final double rate = 7.80; public static void main (String [ ] args) { double USdollar = ; double HKdollar = USdollar * rate; System.out.println( "US" + USdollar + " -> HK" + HKdollar); } rate 7.80 Another local variable declaration with initialization a Michael Fung, CS&E, The Chinese University of HK
29
Michael Fung, CS&E, The Chinese University of HK
Thomas Cook class UStoHKConverter { static final double rate = 7.80; public static void main (String [ ] args) { double USdollar = ; double HKdollar = USdollar * rate; System.out.println( "US" + USdollar + " -> HK" + HKdollar); } rate 7.80 Print the result a Michael Fung, CS&E, The Chinese University of HK
30
Michael Fung, CS&E, The Chinese University of HK
Thomas Cook class UStoHKConverter { static final double rate = 7.80; public static void main (String [ ] args) { double USdollar = ; double HKdollar = USdollar * rate; System.out.println( "US" + USdollar + " -> HK" + HKdollar); } rate 7.80 Strings Strings a Michael Fung, CS&E, The Chinese University of HK
31
Michael Fung, CS&E, The Chinese University of HK
Result US > HK962.91 a Michael Fung, CS&E, The Chinese University of HK
32
Michael Fung, CS&E, The Chinese University of HK
Type in Expression Type promotion/conversion byte short int long float double byte b = 104; float f = (float) ; double d = 35 * f – 29.7 / b; double a Michael Fung, CS&E, The Chinese University of HK
33
Michael Fung, CS&E, The Chinese University of HK
Type in Expression Type promotion/conversion byte short int long float double byte b = 104; float f = (float) ; double d = 35 * f – 29.7 / b; [double int * float – double / byte] a Michael Fung, CS&E, The Chinese University of HK
34
Michael Fung, CS&E, The Chinese University of HK
Type in Expression Type promotion/conversion byte short int long float double byte b = 104; float f = (float) ; double d = 35 * f – 29.7 / b; [double float * float – double / double] a Michael Fung, CS&E, The Chinese University of HK
35
Michael Fung, CS&E, The Chinese University of HK
Type in Expression Type promotion/conversion byte short int long float double byte b = 104; float f = (float) ; double d = 35 * f – 29.7 / b; [double float – double] a Michael Fung, CS&E, The Chinese University of HK
36
Michael Fung, CS&E, The Chinese University of HK
Type in Expression Type promotion/conversion byte short int long float double byte b = 104; float f = (float) ; double d = 35 * f – 29.7 / b; [double double – double] a Michael Fung, CS&E, The Chinese University of HK
37
Michael Fung, CS&E, The Chinese University of HK
Type in Expression Type promotion/conversion byte short int long float double byte b = 104; float f = (float) ; double d = 35 * f – 29.7 / b; [double double] a Michael Fung, CS&E, The Chinese University of HK
38
Michael Fung, CS&E, The Chinese University of HK
Default Type Integer literals (integer numbers appearing in program code) are generally treated as int type. e.g. i = 7 * j / (-9 + k); e.g. Math.cos(30 * Math.PI / 180); Real number/ floating point number literals (numbers with decimal point or in exponential notation appearing in program code) are generally treated as double type. e.g. p = 7.0 * q ; e.g. Math.toRadians(3e-1 * 100.); 3.0 x 10-1 a Michael Fung, CS&E, The Chinese University of HK
39
Michael Fung, CS&E, The Chinese University of HK
Summary Common conventions and nomenclature Java program structure Data item declaration type and identifier field VS local variable a Michael Fung, CS&E, The Chinese University of HK
40
Michael Fung, CS&E, The Chinese University of HK
Summary Type in Java 8 primitive types class types (to be discusses later) Assignment statement and Expressions a Michael Fung, CS&E, The Chinese University of HK
41
Michael Fung, CS&E, The Chinese University of HK
End Note Readings and References Section 2.1 – 2.5 Exercise 1.3, 1.4, 1.16, 1.17, 1.18, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7 Programming Projects 2.1 a Michael Fung, CS&E, The Chinese University of HK
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.