0% found this document useful (0 votes)
13 views8 pages

Unit 1 Java Programming With Linux

Uploaded by

aaliyahameed498
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

Unit 1 Java Programming With Linux

Uploaded by

aaliyahameed498
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

UNIT 1 - Contents

1. Genesis of Java
2. Characteristics of Java
3. Program Structure
Unit 1 4. Java Tokens
✓ Identifiers
JAVA PROGRAMMING ✓ Literals
✓ Variables
✓ Data Types
✓ Operators

2 September 2021 1 2 September 2021 2

Genesis of Java Comparison with C and C++


• General Purpose Object Oriented • No typedefs, #define or pre-processor.
Language • No structure and unions
• No enums
• Developed by Sun Microsystems in 1991
• No stand-alone functions
• Originally called OAK • No multiple inheritance
• Team Members – James Gosling, Patrick • No goto statements
Naughton • No operator overloading
• Story Behind JAVA • No pointers

2 September 2021 3 2 September 2021 4

Java Programming Language Java Platform – 2 Components


• Java is both compiled and interpreted. 1. Java Virtual Machine
2. Java Application Programming Interface (Java
Source Compiler Byte Code API)
Code
API – consists of hundreds of classes and
packages
Java Virtual
Byte Interpreter Machine Output
Code (JVM)

2 September 2021 5 2 September 2021 6


Java API Packages Java API Packages
• Language support package - collection of • Networking package – collection of classes for
classes and methods required for communicating with other computers via
implementing basic features of Java. Internet.
• Utilities package – collection of classes to • AWT package – Abstract Window Toolkit
provide utility functions such as date and time Package contains classes that implements GUI.
functions. • Applet package – Includes a set of classes that
• Input/Output package – collection of classes allows to create java applets.
required for I/O manipulation.

2 September 2021 7 2 September 2021 8

Java Environment Types of Java Programs


• Java API • Stand alone applications
• Java Development Kit (JDK)
JDK
❑javac – compiler which translates java source • Web applets
code to bytecode
❑java – interpreter that interprets bytecode
into machine instructions. • Servlets
❑appletviewer – for viewing java applets.
2 September 2021 9 2 September 2021 10

Types of Java Program Characteristics of Java


Java Source • Compiled and interpreted
Code
• Platform independent and portable- can run on
Java Compiler any machine or Operating System.
Applets Application
(Java Type (Java • Object-oriented
Enabled Web Interpreter)
Browser)
• Robust and secure – strict runtime and compile
time checking for data types. Supports exception
Output Output
handling.

2 September 2021 11 2 September 2021 12


Characteristics of Java Characteristics of Java
• Distributed – java applications can access and • High performance- Due to intermediate
open remote objects as they do in a local bytecode and multithreading
system.
• Dynamic – capable of linking new class
• Simple, small and familiar – many features in C libraries, methods and objects.
and c++ which are unreliable are not in java.
To make it familiar, it was modelled on C and
C++.
• Multithreaded and interactive – handling
multiple tasks simultaneously.

2 September 2021 13 2 September 2021 14

Program Structure Program Structure


public – any class can see the main method. • System- built-in class used for output
static – this allows to call a method without creating • out- object of the built-in class System
the object of that class. main() method should be
declared as static since it is called by the
• println() – built-in method used for printing
interpreter before any instances are made. the output. It also prints the newline
character “\n”. (If print() is used instead of
void- not returning any value
println(), then newline character “\n” will not
main- method name
be printed.)
String args[] – array of instances of the class String.

2 September 2021 15 2 September 2021 16

Reserved Keywords Identifiers


abstract continue for new switch • Used for naming classes, methods, variables,
assert default goto package synchronized
labels, packages and interfaces.
boolean do if private this
break double implements protected throw Rules
byte else import public throws 1. Can have alphabets, digits, underscore and $.
case enum instanceof return transient
2. Must not begin with a digit.
catch extends int short try
char final interface static void 3. Uppercase and lowercase are distinct.
class finally long strictfp volatile 4. Can be of any length
const float native super while

2 September 2021 17 2 September 2021 18


Identifiers Literals(Constants)
• Example for invalid identifiers • Sequence of characters, digits, letters and
1more, 3$, a:b, #1, @abc other characters that represent constant
• Java Naming Conventions values to be stored.
1. Class names begin with an uppercase letter and
each subsequent word with an uppercase letter. 1. Integer literals
Eg: JavaTest, PrimeNum 2. Floating point literals
2. All public methods and instance variables begin
with a lowercase letter and each subsequent 3. Boolean literals
word by uppercase letter. Eg: nextItem, 4. Character literals
getMarks()
5. String literals
2 September 2021 19 2 September 2021 20

Literals- Integer literals Literals- Floating point literals


• Any integral numeric value is an integer literal. • Represent decimal values with a fractional
1. Decimal Integer Literal – consists of digits part.
from 0 to 9. Eg: 123, -81 • Can be expressed in standard or scientific
2. Octal Integer Literal – consists of digits from notation.
0 to 7, with a leading zero. Eg: 053, 041
1. Example for standard notation - 3.1419,
3. Hexadecimal Integer Literal – consists of 0.6667.
digits from 0 to 9, alphabets a to f. The
sequence of digits are preceded by 0x or 0X. 2. Example for scientific notation – 2.1565e2
Eg: oxbca, ox345 means 2.1565 x 102

2 September 2021 21 2 September 2021 22

Literals- Boolean literals Literals- Character literals


• Only 2 logical values – true or false • Constants that contain a single character enclosed within
a pair of single quotation mark.
• True is not equal to 1 and false not equal to 0 • Eg: ’a’, ‘2’ etc.
• Backslash character constants – used for escape
sequences and characters that cannot be entered directly.
• \’ – single quote
• \”- double quote
• \\ - backslash
• \n – New line
• \t – Tab space
• \b - Backspace

2 September 2021 23 2 September 2021 24


Literals- String literals Variables
• A sequence of characters enclosed within • Basic unit of storage in a Java program.
• Defined as a combination of an identifier, type and
double quotes. scope.
• Example – “Java” ❑ Rules
1. Must not begin with a digit.
2. Uppercase and lowercase are distinct.
3. It should not be a keyword.
4. Whitespace is not allowed.
5. Can be of any length.
Example: int x,
float a,b
2 September 2021 25 2 September 2021 26

Variables - Scope Data Types


class scope{ ❖ Integers
➢ byte – 1 byte
public static void main(String args[ ])
➢ short – 2 bytes
{ ➢ int – 4 bytes
int b=1; ➢ long – 8 bytes

{ int b=2; //creates compile time error ❖ Floating-point numbers


➢ float – 4 bytes
} ➢ double – 8 bytes
} ❖ Characters – 2 bytes
} ❖ Boolean – 1 byte

2 September 2021 27 2 September 2021 28

Data Types Casting


int three=3; • Automatic conversion – when the destination
char one=‘1’; variable has enough precision to hold the
char four = (char)(three+one); source value (widening). Eg: byte a;
Result int b=a
four=‘4’ • Narrowing – storing an integer variable into a
byte variable. In this case casting is required.
Example: int a=100
byte b =(byte) a
2 September 2021 29 2 September 2021 30
Operators Arithmetic Operator
1. Arithmetic operators • + Addition
2. Relational operators • - Subtraction
3. Logical operators • * Multiplication
4. Assignment operators • / Division
5. Increment & Decrement operators
• % Modulus
6. Conditional operators
7. Bitwise operators
8. Special operators

2 September 2021 31 2 September 2021 32

Relational Operator Logical Operator


• < Less than • && - Logical AND
• <= Less than or Equal to • || - Logical OR
• > Greater than • ! – Logical Not
• >= Greater than or Equal to Example: if(age>55 && salary >10000)
• == Equal to
• != Not Equal to

2 September 2021 33 2 September 2021 34

Assignment Operator Increment & Decrement Operator


• = • ++
• Short hand assignment operator • --
• += Example : a++, ++a
• -=
• *=
• /=
Example : a+=1

2 September 2021 35 2 September 2021 36


Conditional Operator Bitwise Operators
Syntax: • Used for testing the bits or shifting them to
exp1 ? exp2:exp3 where exp1, exp2 and exp3 the right or left.
are expressions. • & - Bitwise AND
exp1 is evaluated first and if it is True, exp2 is • | - Bitwise OR
evaluated and becomes the value of the • ^ - Bitwise Exclusive OR
conditional expression. • ~ - 1’s Complement (NOT Operator)
If exp1 is False, exp3 is evaluated and its value • << - Shift left
becomes the conditional expression.
• >> - Shift right
Example: x=(a>b)?a:b a=10, b=15
2 September 2021 37 2 September 2021 38

Bitwise Operators Bitwise Operators


Bitwise AND operator(&) Bitwise Exclusive OR operator(^)
00101010 00101010
00001111 00001111
00001010 -------Result 00100101 -------Result
Bitwise OR operator(|) Bitwise NOT operator(~)
00101010 00101010
00001111
00101111--------Result 11010101--------Result

2 September 2021 39 2 September 2021 40

Bitwise Operators Special Operators


Bitwise shift LEFT operator(<<) • instanceof
00101010 <<2 Example: if (Student instanceof(person))
01010100
10101000 -------Result • Member selection operator(.)
Bitwise shift RIGHT operator(>>)
00101010 >>2
Example: Student.age
00010101
00001010--------Result

2 September 2021 41 2 September 2021 42


Operator Precedence – Highest to
Lowest SAMPLE PROGRAMS
1. (), []
2.
3.
++, -- ,~ , !
*, /, %
• Command Line Programs
4. +, - 1. Find the sum of two numbers.
5. >>, <<
6. >, >=, <, <=
7. ==, !=
8. &
9. ^
10. |
11. &&
12. ||
13. ?:
14. =, op=

2 September 2021 43 2 September 2021 44

Assignment Assignment
• Control Statements • Control Statements
✓ Looping statements
✓Branching statements 1. While loop
1. If statement 2. Do while loop
3. For loop
2. If…else statement ✓ Jump statements
3. Nested if statement 1. Break
2. Continue
4. Switch statement
3. return
✓ Labelled loops (with continue)

2 September 2021 45 2 September 2021 46

Assignment
• Control Statements
✓ Labelled loops (with continue)
Example Program
class ContinueLabel{
public static void main(String args[])
{
outer: for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(j>i)
{ System.out.println(" ");

}
continue outer;
End of Unit 1
System.out.print(" "+(i*j));
}
}}}

2 September 2021 47 2 September 2021 48

You might also like