Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 27

Java Platform, Program

Entry Point, Variables,


Operators, Console
Output
IT Academy
Agenda
 Introduction to Java Platform
 Install JDK (Java Development Kit)
 The Entry Point into the Program
 Console Output data
 Compile and Run Java Program
 Variables and Types
 Type Casting
 Arithmetic and Bitwise Operators
Introduction to Java Platform

• James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language
project in June 1991.
• The language was initially called Oak. Now, Java it is the name of a platform
and language.
• Java applications are typically compiled to bytecode (class file) that can run on
any Java Virtual Machine (JVM) regardless of computer architecture.
• The original and reference implementation Java compilers, virtual machines,
and class libraries were developed by Sun from 1995 (May 23, 1995, Java 1.0).
• With the advent of Java 2 (JDK 1.2, released initially as J2SE 1.2 in December
1998–1999), new versions had multiple configurations built for different types
of platforms.
Introduction to Java Platform
• Specification Java 2 (JDK 1.2) – 1998/1999 (collection support; Swing; reflection;
Unicode; JIT compiler). Sun renamed new J2 versions as Java EE, Java ME, and
Java SE.
• Oracle Corporation is the current owner of the official implementation of the
Java SE platform, following their acquisition of Sun Microsystems on January
27, 2010.
• This implementation is based on the original implementation of Java by Sun.
• The Java 13 was released on September 17, 2019 and includes the following
new features, as well as "hundreds of smaller enhancements and thousands of
bug fixes".
• The Oracle implementation is available for Microsoft Windows, macOS, Linux,
and Solaris.
https://www.geeksforgeeks.org/the-complete-history
-of-java-programming-language/
Java Platform Components

• Java Tools and Commands Reference:


https://docs.oracle.com/javase/9/tools/tools-and-command-reference.htm#JSWOR596
Install Java JDK
http://www.oracle.com/technetwork/java/javase/downloads/index.html

java -version
First Java Program
Compile and Run Java Program
Java Archive
manifest.mf Example.jar

jar cfm Example.jar manifest.mf Example.class java -jar Example.jar

Compile Bytecode JVM

Example.java Example.class Executing

javac Example.java java Example


Compile and Run Java Program

• For Compile files:


 javac -d out src\*.java
• For Running compiled file:
 java Main
• For Creating Java archive:
 jar cfm Example.jar manifest.mf Main.class
• For Running Java archive:
 java -cp Example.jar Main
 java -jar Example.jar
Java Program Entry Point
• One of the class must have the main() method as the execution starting point of the
program:
public class Main {
public static void main(String[] args) {
System.out.println("Inside the method main()");
}
}

• It must be static because, before your program starts, there aren’t any objects to
send messages to.
• This is a static context (a class static method):
̶ you can send messages to objects, if you have some objects;
̶ you cannot send a message to yourself, or use any instance variables – this is a static
context, not an object.
Setting an Entry Point

• The parameter of the main() method is an array of type String, which contains the
additional arguments after the class name argument in the "java" command line.
public static void main(String[] a) {
System.out.println("Number of arguments = "+a.length);
for (int i=0; i<a.length; i++) {
System.out.println(" a["+i+"] = " + a[i]);
}
}

• You can directly invoke this application by running the following command:
$ java -jar HelloJava.jar hello java!
Number of arguments = 2
a[0] = hello
a[1] = java!
Java Program Console Output
• For displaying simple text messages to the user:
 System.out.print(...) – prints the required output on the same line
continuously again and again on the screen;
 System.out.println(...) – prints the output in the next line of the previous
result on the screen.
public class Main {
public static void main(String[] args) {
System.out.print("Hello ");
System.out.print("Java!");
System.out.println(" How are you?");
System.out.println("Goodbye Java!");
}
}
Java Comments
• The Java language supports three kinds of comments:
• the compiler ignores everything from // to the end of the line.
// This example demonstrates the use of single line comments

• the compiler ignores everything from /* to */.


/* This is a sample class which is used to demonstrate the use
of multi-line comments. This comment does not appear in the
java documentation */

• /** and */ indicates a documentation comment. The compiler ignores this kind
of comment, just like it ignores comments that use /* and */.
• The JDK javadoc tool uses documentation comment when preparing automatically
generated documentation.
Java Variables
 Variable – symbolic name associated with a value and whose associated value
may be changed.
 Declaration – process of variable's specifying. Usually declaration consist of
defining: type, name and default value of variable.

int a; // declares variable 'a' of 'int' type


a = 194; // example of initialization
int b = 10; // declares and initialization
int c, x, y = 10; // declares three variables and initialization one of them
byte d = 22; // declares variable 'd' of 'byte' type
double pi = 3.14159; // declares and assigns value of PI
char e = 'a'; // the 'char' variable 'e' is initialized
// with value 'a'

 A process in which a variable is set to its first value is called initialization.


Declaration and Initialization
Java Primitive Types
Type Size (bits) Value

byte 8 -128 < x < 127

short 16 -32768 < x < 32767

int 32 -2147483648 < x < 2147483647

long 64 -922372036854775808 < x < 922372036854775807

char 16 0 < x < 65536

float 32 3,4e-38 < |x| < 3,4e38; 7-8 digits

double 64 1,7e-308 < |x| < 1,7e308; 17 digits

boolean JVM depends false, true


Java Literals
• A literal is a source code representation of a fixed value.
• They are represented directly in the code without any computation.
• Literals can be assigned to any primitive type variable.

Value Type
10 Integer
25.38 Floating point
'A' '/u0040' Unicode character
"Java is programming language" String
true Boolean value
Java Escape Sequences

• Java language supports few special escape sequences for String and char
literals as well.

Notation Character represented


\n Newline (\u000a)
\r Carriage return (\u000d)
\t Tab (\u0009)
\" Double quote (\u0022)
\' Single quote (\u0027)
\\ Backslash (\u005c)
Implicit vs Explicit Type Casting
• An implicit casting is automatic when no loss of information is possible.
int i = 100;
long l = i; //no explicit type casting required
float f = l; //no explicit type casting required

• An explicit cast required when there is a potential loss of accuracy.


double d = 100.04;
long l = (long) d; //explicit type casting required
int i = (int) l; //explicit type casting required
Implicit vs Explicit Type Casting
public static void main(String[] args) {
short s = 259;
byte b = (byte)s;
System.out.println("Variable 's' = " + s);
System.out.println("Variable 'b' = " + b);
}

Variable 's' = 259


Variable 'b' = 3
Java Arithmetic Operators

• Arithmetic operators are used in mathematical expressions in the same way


that they are used in algebra.
Example
Operator Description
(a = 10, b = 20)

+ (addition) Adds values on either side of the operator. a + b will give 30

Subtracts right-hand operand from left-hand


- (subtraction) a - b will give -10
operand.

* (multiplication) Multiplies values on either side of the operator. a * b will give 200

/ (division) Divides left-hand operand by right-hand operand. b / a will give 2

Divides left-hand operand by right-hand operand


% (modulus) b % a will give 0
and returns remainder.
Increment and Decrement Operators

• Increment and decrement operators used to increment or decrement a value by 1.


• There are two varieties of increment and decrement operators:
• Post-Increment / Post-Decrement :
value is first used for computing the result and then incremented / decremented.
• Pre-Increment / Pre-Decrement :
value is incremented / decremented first and then result is computed.

int a = 4; // a = 4
a++; // a = 5
int b = a++; // a = 6 b = 5
b = ++a; // a = 7 b = 7
Java Bitwise Operators
• The Java language introduces the bitwise operators, which help in
manipulating a single bit of a byte.

Operator Description
& Bitwise AND
Truth table

| Bitwise OR a b a&b a|b a^b

^ Bitwise XOR 0 0 0 0 0
0 1 0 1 1
>> Shift right
1 0 0 1 1
>>> Shift right zero fill 1 1 1 1 0

<< Shift left


Left-Shift << Operator
• Left-shift works as follows:
int a = 64, b = 23;
Variable 'a' = 128
a = a << 1; // 64 * 2^1
Variable 'b' = 184
b = b << 3; // 23 * 2^3
Right-Shift >> and >>> Operators
• Right-shift works as follows:
int a = 128, b = 174;
Variable 'a' = 64
a = a >> 1; // 128 / 2^1
Variable 'b' = 21
b = b >> 3; // 174 / 2^3
Right-Shift >> and >>> Operators
• Right-shift works as follows:
int a = -64; // (-)1 1111111 11111111 11111111 11000000
a = a >> 1; // (-)1 1111111 11111111 11111111 11100000

int b = -64;
b = b >>> 1; // (+)0 1111111 11111111 11111111 11100000

Variable 'a' = -32


Variable 'b' = 2147483616
Thanks for
attention!
IT Academy

You might also like