02 basic java programming and operators

43
Module 02 – Basic Java Programming Danairat T. Line ID: Danairat FB: Danairat Thanabodithammachari +668-1559-1446

Upload: danairat-thanabodithammachari

Post on 15-Apr-2017

840 views

Category:

Software


0 download

TRANSCRIPT

Page 1: 02 basic java programming and operators

Module 02 – Basic Java Programming

Danairat T.

Line ID: Danairat

FB: Danairat Thanabodithammachari

+668-1559-1446

Page 2: 02 basic java programming and operators

Fundamental Java ProgrammingThe Course Outline

Module 01 – Introduction to Java

Module 02 – Basic Java Programming

Module 03 – Control Flow and Exception Handling

Module 04 – Object Oriented in Java

Module 05 – Java Package and Access Control

Module 06 – Java File IO

Module 07 – Java Networking

Module 08 – Java Threading

Page 3: 02 basic java programming and operators

Module 02 – Basic Java Programming and Operators

• Basic Java Programming

• Deploying Java Application

• Building Java Archive File

• Executing Basic Java Application

• Monitoring Basic Java Application

• Java Operators

Page 4: 02 basic java programming and operators

Java File (ASCII Source Code) and Class File (Binary)

In the Java programming language, all source code is first

written in plain text files ending with the .java extension.

Those source files are then compiled into .class file

(bytecode) by the javac compiler.

The java launcher tool then runs your application with an

instance of the Java Virtual Machine.

Page 5: 02 basic java programming and operators

JVM (Java Virtual Machine)

Because the Java VM is available on many different operating systems, the same .class files are capable of running on Microsoft Windows OS, the Solaris Operating System, Linux, or Mac OS.

Page 6: 02 basic java programming and operators

The Java Platform

The Java platform has two components:

• The Java Virtual Machine

• The Java Application Programming Interface (API)

The API is a large collection of ready-made software components that provide many useful capabilities. It is grouped into libraries of related classes and interfaces; these libraries are known as packages.

Page 7: 02 basic java programming and operators

A Closer Look at the "Hello World!" Application

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello World");

}

}

Class Definition The main Method

The System class from the core library to print the "Hello World!" message to standard output. System class contains member called “static PrintStream out” to use as a "standard" output stream.

Page 8: 02 basic java programming and operators

LAB – Building your first Application with JDeveloper

1.) Click “New ApplicationF” from Jdeveloper 2.) Enter your “Application Name”,Select “Application Template”, Click ”Next”

Page 9: 02 basic java programming and operators

LAB – Building your first Application with JDeveloper

3.) (Optional) Enter “Application Package Prefix”to organize the structure of Java class files.

4.) Click “Finish”

Page 10: 02 basic java programming and operators

LAB – Building your first Application with JDeveloper

5.) Right Click “NewF” from the Project 6.) Select “Java Class”, Click “OK”

Page 11: 02 basic java programming and operators

LAB – Building your first Application with JDeveloper

7.) Enter “Name” of the class, Check “Main Method”

8.) Click “OK”

Page 12: 02 basic java programming and operators

LAB – Building your first Application with JDeveloper

9.) Write codes in the main methodSystem.out.println(“Hello World”);

10.) Click “Run” button.

Page 13: 02 basic java programming and operators

LAB – Building your first Application with JDeveloper

11.) See the result from the console output 12.) (Optional) Review the folder structurefor java source code file

Page 14: 02 basic java programming and operators

LAB – Building your first Application with JDeveloper

13.) (Optional) Review the file output and Application Directory Structure

Page 15: 02 basic java programming and operators

Deploying Java Application to execute outside the

IDE with jar command line

Java provides a capability to export your class files into a single cross platform archive file named Java™ Archive (JAR).

JAR file contains the class files and auxiliary resources associated

1.) We first create a text file named Manifest.txt with the following contents:

Main-Class: MyPackage.MyClass

2.) We then create a JAR file named MyJar.jar by entering the following command:

jar cfm MyJar.jar Manifest.txt MyPackage/*.class

3.) When you run the JAR file with the following command, the main method of MyClass executes: -

java -jar MyJar.jar

Page 16: 02 basic java programming and operators

The jar command line options

Operation Command

To create a JAR file jar cf jar-file input-file(s)

To view the contents of a JAR file jar tf jar-file

To extract the contents of a JAR file jar xf jar-file

To run an application packaged as a

JAR file (requires the Main-class manifest

header)

java -jar app.jar

Page 17: 02 basic java programming and operators

LAB - Creating Java Archive File (JAR) and Run application

outside the IDE

1.) Click “NewF” from the project menu 2.) Select “JAR File”, click “OK”

Page 18: 02 basic java programming and operators

LAB - Creating Java Archive File (JAR) and Run application

outside the IDE

3.) Enter your application name “my_first_app” 4.) Click “Browse” to select the started class

Page 19: 02 basic java programming and operators

LAB - Creating Java Archive File (JAR) and Run application

outside the IDE

5.) Select the “Main Class Name:” which is “Class1” 6.) Click “OK”

Page 20: 02 basic java programming and operators

LAB - Creating Java Archive File (JAR) and Run application

outside the IDE

7.) Select “Deploy” from the project menu 8.) Click “Next”

Page 21: 02 basic java programming and operators

LAB - Creating Java Archive File (JAR) and Run application

outside the IDE

9) Click “Finish” 10.) Review the result jar file “my_first_app.jar”

Page 22: 02 basic java programming and operators

LAB - Creating Java Archive File (JAR) and Run application

outside the IDE

11.) Using Command Line Client to execute the JAR file

D:\JDeveloper\mywork\Application2\Project1\deploy>java -jar my_first_app.jar

Page 23: 02 basic java programming and operators

Monitoring Basic Java Application

Using JConsole

The JConsole graphical user interface is a monitoring tool that complies to the Java Management Extensions (JMX) specification. JConsole uses the extensive instrumentation of the Java Virtual Machine (Java VM) to provide information about the performance and resource consumption of applications running on the Java platform.

Starting JConsole

The jconsole executable can be found in JDK_HOME/bin, where JDK_HOME is the directory in which the Java Development Kit (JDK) is installed. If this directory is in your system path, you can start JConsole by simply typing jconsole in a command (shell) prompt.

Page 24: 02 basic java programming and operators

LAB – Monitoring Java Application with JConsole

1.) Create Java Application 2.) Deploy Java Application

Adding the loop into the code for long running app simulation.

for (int i=1; i<1000000; i++) {

System.out.println("Hi");

}

Page 25: 02 basic java programming and operators

LAB – Monitoring Java Application with JConsole

3.) Execute Java Application>java -jar my_first_app.jar

4.) Start Jconsole and select your targeted Java process>jconsole

Page 26: 02 basic java programming and operators

LAB – Monitoring Java Application with JConsole

5.) Review Monitoring Results

Page 27: 02 basic java programming and operators

LAB – Monitoring Java Application with JConsole

6.) Exit the JConsole

Page 28: 02 basic java programming and operators

Java Class

A class is a blueprint or prototype from which objects are created. This section defines a class that models the state and behavior of a real-world object. It intentionally focuses on the basics, showing how even a simple class can cleanly model state and behavior.

Page 29: 02 basic java programming and operators

Java Object

Java Object is conceptually similar to real-world objects. They consist of state and behavior.

An object stores its state in fields (variables) and exposes its behavior through methods.

Methods operate on an object's internal state. Hiding internal state is known as data encapsulation

Class

Object 1

Object 2

BlueprintInstance

Page 30: 02 basic java programming and operators

Java Variables

Class Variables (Static Fields)

The field member are declared as static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated.

Instance Variables (Non-Static Fields)

The field members are unique to each instance of a class the instance.

Local Variables

The variable is declared in a method. Local variables are only visible to the methods. they are not accessible from the rest of the class.

Page 31: 02 basic java programming and operators

Java Language Keywords

abstract continue for new switch

assert*** default goto* package synchronized

boolean do if private this

break double implements protected throw

byte else import public throws

case enum**** instanceof return transient

catch extends int short try

char final interface static void

class finally long strictfp** volatile

const* float native super while

*not used

**added in 1.2

***added in 1.4

****added in 5.0

Naming Convention:-

Do not use the Java Keywords as you variable. Normally, The variable should start with lowercase and follow by uppercase for the next word, eg. custArray, printManager

Page 32: 02 basic java programming and operators

Java Primitive Data TypesDefault Value and Wrapper Class

The IEEE 754 Standard

The wrapper classes help you to perform conversion; eg converting String to integer:-

Interger.valueOf(“15”).intValue(); or int j = Integer.parseInt("10"); Please see java.lang.* library

Type Contains Default Size RangeWrapper

Class

boolean true or false false 1 bit NA Boolean

char Unicode character \u0000 16 bits \u0000 to \uFFFF Character

byte Signed integer 0 8 bits -128 to 127 Byte

Short

short Signed integer 0 16 bits -32768 to 32767 Integer

int Signed integer 0 32 bits -2147483648 to 2147483647 Long

long Signed integer 0 64 bits-9223372036854775808 to

9223372036854775807 Character

float IEEE 754 floating point 0.0 32 bits ±1.4E-45 to ±3.4028235E+38 Float

double IEEE 754 floating point 0.0 64 bits±4.9E-324 to

±1.7976931348623157E+308 Double

String is not a part of Java primitive data types.

Page 33: 02 basic java programming and operators

Escape Character

Escape Sequence Character Value

\b Backspace

\t Horizontal tab

\n Newline

\f Form feed

\r Carriage return

\" Double quote

\' Single quote

\\ Backslash

\uxxxx

The Unicode character with encoding xxxx, where

xxxx is four hexadecimal digits. Unicode escapes

can appear anywhere in a Java program, not only

in character and string literals.

Page 34: 02 basic java programming and operators

Primitive Type Conversion

Convert

From:

Convert To:

boolean byte short char int long float double

boolean - N N N N N N N

byte N - Y C Y Y Y Y

short N C - C Y Y Y Y

char N C C - Y Y Y Y

int N C C C - Y Y* Y*

long N C C C C - Y* Y*

float N C C C C C - Y

double N C C C C C C -

The letter N in the table means that the conversion cannot be performed. The letter Y means that the conversion is a widening conversion and is therefore performed automatically and implicitly by Java. The letter C means that the conversion is a narrowing conversion and requires an explicit cast. Finally, the notation Y* means that the conversion is an automatic widening conversion, but that some of the least significant digits of the value may be lost by the conversion.

Page 35: 02 basic java programming and operators

Java Operators

Operators Precedence

postfix expr++ expr--

unary ++expr --expr +expr -expr ~ !

multiplicative * / %

additive + -

shift << >> >>>

relational < > <= >= instanceof

equality == !=

bitwise AND &

bitwise inclusive OR |

logical AND &&

logical OR ||

ternary ? :

assignment = += -= *= /=

Page 36: 02 basic java programming and operators

LAB - ArithmeticDemo

class ArithmeticDemo {

public static void main (String[] args){

int result = 1 + 2; // result is now 3

System.out.println(result);

result = result - 1; // result is now 2

System.out.println(result);

result = result * 2; // result is now 4

System.out.println(result);

result = result / 2; // result is now 2

System.out.println(result);

result = result + 8; // result is now 10

result = result % 7; // result is now 3

System.out.println(result);

}

}

Page 37: 02 basic java programming and operators

LAB - ConcatDemo

class ConcatDemo {

public static void main(String[] args){

String firstString = "This is";

String secondString = " a concatenated string.";

String thirdString = firstString+secondString;

System.out.println(thirdString);

}

}

Page 38: 02 basic java programming and operators

LAB - UnaryDemo

class UnaryDemo {

public static void main(String[] args){

int result = +1; // result is now 1

System.out.println(result);

result--; // result is now 0

System.out.println(result);

result++; // result is now 1

System.out.println(result);

result = -result; // result is now -1

System.out.println(result);

boolean success = false;

System.out.println(success); // false

System.out.println(!success); // true

}

}

Page 39: 02 basic java programming and operators

LAB - PrePostDemo

class PrePostDemo {

public static void main(String[] args){

int i = 3;

i++;

System.out.println(i); // "4"

++i;

System.out.println(i); // "5"

System.out.println(++i); // "6"

System.out.println(i++); // "6"

System.out.println(i); // "7"

}

}

Page 40: 02 basic java programming and operators

LAB - ComparisonDemo

class ComparisonDemo {

public static void main(String[] args){

int value1 = 1;

int value2 = 2;

if(value1 == value2) System.out.println("value1 == value2");

if(value1 != value2) System.out.println("value1 != value2");

if(value1 > value2) System.out.println("value1 > value2");

if(value1 < value2) System.out.println("value1 < value2");

if(value1 <= value2) System.out.println("value1 <= value2");

}

}

Page 41: 02 basic java programming and operators

LAB - ConditionalDemo1

class ConditionalDemo1 {

public static void main(String[] args){

int value1 = 1;

int value2 = 2;

if((value1 == 1) && (value2 == 2)) System.out.println("value1 is 1 AND value2 is 2");

if((value1 == 1) || (value2 == 1)) System.out.println("value1 is 1 OR value2 is 1");

}

}

Page 42: 02 basic java programming and operators

LAB – ConditionalDemo2

class ConditionalDemo2 {

public static void main(String[] args){

int value1 = 1;

int value2 = 2;

int result;

boolean someCondition = true;

result = someCondition ? value1 : value2;

System.out.println(result);

}

}

Page 43: 02 basic java programming and operators

Danairat T.

Line ID: Danairat

FB: Danairat Thanabodithammachari

+668-1559-1446

Thank you