1 2. Java language basics (1)Minhaeng Lee
2 Contents Features Create your first java program Packages TypePrimitive Types Useful Class Types Casting Conversion (Integer/Float/Double to String, vise versa)
3 Feature of Java Java Language Its own syntax rule, structuresObject-Oriented Programming concepts (OOP) Much like C++ language Code block modularized into method (= function) Delimited by braces { } Statement ends with semi-colon ;
4 Feature of Java The Java Compiler JVM Source code .javaCompile ($javac test.java) Byte code .class targeting for JVM Running ($java test) $ means command prompt (e.g. c:\Users\Name> ) JVM Heart of java for "write-once, run-anywhere” We can run java code anywhere if JVM is available
5 Java Virtual Machine Using a single sourceWorking on different platform
6 Features of Java Garbage Collector Java Development Kit (JDK)Memory management unit Automatically prevent memory leak Release memory automatically c.f. C/C++ language, user have to explicitly release it Java Development Kit (JDK) Including compiler and additional libraries Java Runtime Environment (JRE) Including JVM and libraries for running
7 JDK, JRE, and JVM
8 JDK, JRE, and JVM javaw Similar role with java but not waiting until given application is finished javap Feed .class and print method and variables jar Generate jar (java library) JIT Convert bytecode into machine language in running time. Fast b/c uses caching Interpret type of language property Bytecode Lower programming language than java or other programming language
9 Create your first java program using console/terminalMost basic way of running java Sometimes, we cannot use editing tools like Eclipse (e.g. pure linux environment) Check whether you can run java on your terminal/console Windows : start – execution – cmd OSX : spotlight search – terminal Type “java” and enter to check whether it is installed or not
10 Create your first java program Check whether you have java or notIf you can see this kinds of output, then we can use java in console environment!
11 Type! test.java file Editing tools Display folder contents Mac OSXMac OSX : ls Windows : dir Directory change (common) cd
12 Run! Filename should be similar to class name One class per one fileMain function has same header Reference:
13 Trouble Shooting Javac is unrecognized (Windows)?Need to add path to use command in console Find javac path (something like “C:\Program Files\Java\jdk
14 Programming using EclipseWorkspace directory setting Cannot open more than one Eclipses for a single path Beginning screen Just click “workbench”
15 Programming using Eclipse Create projectRightclick in Project Explorer space New - Project
16 Programming using Eclipse Select project typeWe are going to use Java Project
17 Programming using EclipseSet project Name as you wish Just using default JRE Sometimes, syntax differs based on JRE version
18 Programming using EclipseSet Output Folder Folder for Class file (binary) We just use default location
19 Programming using Eclipse create a new class fileRight click on “src” New - Class
20 Programming using EclipseSet name of class as you wish
21 Programming using Eclipse
22 Programming using Eclipse Type one more time!In Eclipse, save involves compile that generate binary code (.class file) * near Editor tab means “mofieid”
23 Programming using Eclipse Run it!
24 Programming using Eclipse other ways of runningRightclick on java file Run As – Java Application Bottom arrow right side of run icon Run As – Java Application
25 More tips : show line numbersWindows – preferences – General – Editors – Text Editors Check show line number Easy to follow up Java error messages involves line number
26 Example error
27 Packages Located at the top of each source fileSort of source code group Make it well organized (e.g. like folders) Don’t force you to use packages • orgType is the organization type such as com, org, or net. • orgName is the name of the organization's domain, such as makotogroup, sun, or ibm. • appName is the name of the application, abbreviated. • compName is the name of the component.
28 Comments // : comment a single line /* : comment beginEclipse short cut – Ctrl + / /* : comment begin */ : comment ending // this line commented /* All of this par is commented.. */
29 Binary Number Internal data storage is binary numberImportant to understand data overflow and memory management
30 Binary Number (practice)Convert from Binary to Decimal 101 1101 1111 Convert from Decimal to Binary 10 100 128 1024
31 Note! Each statement runs from right to left.
32 Variable Types Variable : word block to store dataE.g. a = 3; Must begin with characters (by syntax) Camel Case (Suggested for readability ) Lower case at the beginning, Upper case at the beginning of each word E.g. hiMyNameIsCamelCase Declaration is needed before we use variable Anywhere before we use C.f In C language, all declarations are needed at the beginning of code
33 Primitive Types Most basic type defined in Java Begins with lower caseQuite common name comparing to other languages int / short Contain integer (4byte, 2 byte) float / double Contains real number (4, 8 byte respectively) float myFloat = 0.3f; double myDouble = 0.3; Boolean (1 byte) Contains true or false. E.g. boolean isNice = true; char (2 byte) One character Using single quotation ‘’
34 Casting Data transfer between different typesE.g. from integer to float / double Use “(
35 Casting (practice) int a; float b; double c; boolean d;int to double : (double) a double to float : (float)c float to int : (int) b int to boolean : (boolean) a (not possible)
36 Useful Class Types String From other types to StringUse double quotation “” Can store sequence of characters Concatenation using + LONG = “small” + “small”; (LONG = “smallsmall”) From other types to String Use toString Every class have Every class can be printed as String type
37 Conversion Inbuilt explicit method in Java From String to numberInteger to String String “1000” to Integer 1000 From String to number
38 Conversion (practice)int to String float to String double to String String to int String to float String to double Integer to int Float to float Double to double
39 Math Notation Operator Math functions + : plus - : minus* : multiplication / : division % : remaining values Math functions Math.
40 System.out.println() Super famous! Standard output of JavaPlease memorize.. I’ve used that more than 10k times.. new line involved System.out.println(“Anything you want!”); int age = 10; System.out.println(“I am “+age+” years old”);
41 String[] args Argument string array ([]) (can be changed)Parameter (or argument) set in main method Useful to change parameters without compiling Java test a b c args[0] : “a” args[1] : “b” args[2] : “c”
42 Practice! Let’s make root equation!We have three parameters come from “args”
43 References IBM