Lecture 1 Introduction to Java Erick Pranata © Sekolah Tinggi Teknik Surabaya 1
» Getting Started with Java » Variables, Constants, Data Types and Operators » Arrays » Control Flow Statements » Misc. 2 © Sekolah Tinggi Teknik Surabaya
3
» Java Standard Edition (Java SE) ˃J2SE can be used to develop client-side standalone applications or applets. » Java Enterprise Edition (Java EE) ˃J2EE can be used to develop server-side applications such as Java Servlets and Java Server Pages. » Java Micro Edition (Java ME). ˃J2ME can be used to develop applications for mobile devices such as cell phones. 4 © Sekolah Tinggi Teknik Surabaya
» TextPad » jEdit » jCreator » JBuilder » Eclipse » NetBeans » Etc. 5 © Sekolah Tinggi Teknik Surabaya
» Java Development Kit » Java Runtime Environment ˃Java Virtual Machine 6 © Sekolah Tinggi Teknik Surabaya
7 Something.java Something.class
class Something { public static void main(String[] args) { System.out.println(“Hello World!"); } 8 © Sekolah Tinggi Teknik Surabaya
9
» Started with a letter or dollar sign “$” or underscore “_” ˃ line, _line, $line » Subsequent characters may be letters, digits, dollar signs, or underscore characters » Variable names must be in mixed case starting with lowercase ˃ line, inputLine, i, j, currNode 10 © Sekolah Tinggi Teknik Surabaya
» Must be all uppercase using underscore to separate words ˃ final int NUMBER_OF_HOURS_IN_A_DAY = 24; 11 © Sekolah Tinggi Teknik Surabaya
Data TypesRemark Default Value byte8-bit signed: -128 to 127 (inclusive)0 short16-bit signed: to (inclusive)0 int 32-bit signed: -2,147,483,648 to 2,147,483,647 (inclusive) 0 long 64-bit signed: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (inclusive) 0 float32-bit floating point0.0f double64-bit floating point0.0d boolean true or false false char16-bit unicode character'\u0000' String- null 12 © Sekolah Tinggi Teknik Surabaya
» The source code representation of a fixed value; literals are represented directly in your code without requiring computation. » Example boolean result = true; char capitalC = 'C'; byte b = 100; short s = 10000; int i = ; 13 © Sekolah Tinggi Teknik Surabaya
// The number 26, in decimal int decVal = 26; // The number 26, in hexadecimal int hexVal = 0x1a; // The number 26, in binary int binVal = 0b11010; 14 © Sekolah Tinggi Teknik Surabaya
double d1 = 123.4; /** * same value as d1, * but in scientific notation **/ double d2 = 1.234e2; float f1 = 123.4f; 15 © Sekolah Tinggi Teknik Surabaya
» The Java programming language also supports a few special escape sequences for char and String literals: ˃\b (backspace) ˃\t (tab) ˃\n (line feed) ˃\f (form feed) ˃\r (carriage return) ˃\" (double quote) ˃\' (single quote) ˃\\ (backslash). 16 © Sekolah Tinggi Teknik Surabaya
long creditCardNumber = 1234_5678_9012_3456L; long socialSecurityNumber = 999_99_9999L; float pi = 3.14_15F; long hexBytes = 0xFF_EC_DE_5E; long hexWords = 0xCAFE_BABE; long maxLong = 0x7fff_ffff_ffff_ffffL; byte nybbles = 0b0010_0101; long bytes = 0b _ _ _ ; » Constraints: ˃At the beginning or end of a number ˃Adjacent to a decimal point in a floating point literal ˃Prior to an F or L suffix ˃In positions where a string of digits is expected 17 © Sekolah Tinggi Teknik Surabaya
OperatorsPrecedence postfixexpr++ expr-- unary++expr --expr +expr -expr ~ ! multiplicative* / % additive+ - shift > >>> relational = instanceof equality== != bitwise AND& bitwise exclusive OR^ bitwise inclusive OR| logical AND&& logical OR|| ternary? : assignment = += -= *= /= %= &= ^= |= >= >>>= 18 © Sekolah Tinggi Teknik Surabaya
19
20 © Sekolah Tinggi Teknik Surabaya
// declares an array of integers int[] anArray; 21 © Sekolah Tinggi Teknik Surabaya
// create an array of integers anArray = new int[10]; // create + initializing int[] anArray = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 }; // accessing System.out.println ("Element 1 at index 0: " + anArray[0]); 22 © Sekolah Tinggi Teknik Surabaya
23
» if-then Statement if (isMoving){ currentSpeed--; } » if-then-else Statement if (testscore >= 90) { grade = 'A'; } else if (testscore >= 80) { grade = 'B'; } else if (testscore >= 70) { grade = 'C'; } else if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; } 24 © Sekolah Tinggi Teknik Surabaya
switch (month) { case 1: monthString = "January"; break; case 2: monthString = "February"; break; case 3: monthString = "March"; break; default: monthString = "Invalid month"; break; } 25 © Sekolah Tinggi Teknik Surabaya
while (count < 11) { System.out.println("Count is: " + count); count++; } do { System.out.println("Count is: " + count); count++; } while (count < 11); 26 © Sekolah Tinggi Teknik Surabaya
for(int i=1; i<11; i++){ System.out.println("Count is: " + i); } 27 © Sekolah Tinggi Teknik Surabaya
28
abstractassertbooleanbreak bytecasecatchchar classconstcontinueDefault dodoubleelseEnum extendsfinalfinallyFloat forgotoifImplements importinstanceofintInterface longnativenewPackage privateprotectedpublicReturn shortstaticstrictfpsuper 29 © Sekolah Tinggi Teknik Surabaya
This page is intentionally left blank © Sekolah Tinggi Teknik Surabaya 30
» Language Basics, ava/nutsandbolts/index.html ava/nutsandbolts/index.html 31 © Sekolah Tinggi Teknik Surabaya