0% found this document useful (0 votes)
27 views

Java Fundamentals

Uploaded by

210801059
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Java Fundamentals

Uploaded by

210801059
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

OCS1902

OOPS USING JAVA

SESSION 1
Java Fundamentals

01
CONTENTS

Introduction-Java Architecture

Data types

Variables
2
Introduction-Java
Architecture

3
● Java, a Programming Language
developed by Sun Micro System in
the year 1991.
● Java was developed by James
Gosling, Patrick Naughton, Chirs
Warth, Ed Frank and Mike Sheridon.
● During the initial period Java was
called as Oak (from 1991 to 1995).
4
● Java is an object oriented
programming language. It has
syntax similar to C and C++
languages, but has vastly improved
features.
● Secure, Portable and Platform
Independent language
-Windows, Mac, Linux
5
JAVA FEATURES
Object-oriented
Secure
Platform Independent/Portable
Architecture-neutral
Robust

6
Object

O
Object

Object

Object

OBJECT
ORIENTED
Object Oriented Programming Language-Everything in
Java is treated as Objects and how they interact with
each other.
7
 No explicit pointer
 Java Programs run inside a virtual machine
sandbox.
 Classloader in Java is a part of the Java
Runtime Environment (JRE) which is used to
load Java classes into the Java Virtual Machine
dynamically. It adds security.

S  Bytecode Verifier: It checks the code


fragments for illegal code that can violate
access rights to objects.
SECURE  Security Manager: It determines what
resources a class can access such as reading
and writing to the local disk.
8
P
PLATFORM
NDEPENDENT

9
 It uses strong memory management.
 Java provides automatic garbage

R collection which runs on the Java Virtual


Machine to get rid of objects which are
not being used by a Java application
ROBUST anymore.
 There are exception handling and the
type checking mechanism in Java.

10
 Java is architecture neutral because there are no
ARCHITECTURE dependent features, for example,

A the size of primitive types is fixed.

 In C programming, int data type occupies 2 bytes of


memory for 32-bit architecture and 4 bytes of memory
ARCHITECTURE for 64-bit architecture. However, it occupies 4 bytes
of memory for both 32 and 64-bit architectures in
NEUTRAL
Java.

11
Basic Steps To Develop a Java Program

Add a main point Add a main point


Elaborate on what you Elaborate on what you
want to discuss. want to discuss.

12
Java Architecture

13
Java Architecture
Step1: Create a java source code with .java extension
Step2: Compile the source code using java compiler, which will
create bytecode file with .class extension.
Step3: Class loader reads both the user defined and library
classes into the memory for execution.
Step4: Bytecode verifier validates all the bytecodes are valid
and do not violate Java’s security restrictions.
Step5: JVM reads bytecodes and translates into machine code
for execution. While execution of the program the code will
interact to the operating system and hardware.

14
5 Phases of Java Program
 Edit: Use an editor to type Java program (Welcome.java)
 Compile: Use a compiler to translate Java program into an
intermediate language called bytecodes, understood by Java
interpreter (Welcome. Class)
 Loading: Use a class loader to read bytecodes from .class file into
memory
 Verify :Use a Bytecode verifier to make sure bytecodes are valid
and do not violate security restrictions
 Execute: Java Virtual Machine (JVM) uses a combination of
interpretation and just-in-time compilation to translate bytecodes
into machine language
 Applications are run on user's machine, i.e. executed by interpreter
with java command (java Welcome) 15
A Simple Java Program

16
Java Fundamentals

17
Basics
 Data Types
 Keywords
 Variables
 Operators
 Conditional Statements
 Loops

18
Datatypes and
Keywords

19
Datatypes
Data Size Minimum Range Maximum Range Default Value
Type (in (for fields)
bits)

byte 8 -128 +127 0


short 16 -32768 +32767 0
int 32 -2147483648 +2147483647 0
long 64 -9223372036854775808 +9223372036854775807 0L

float 32 1.40E-45 3.40282346638528860e+38 0.0f

double 64 4.94065645841246544e- 1.79769313486231570e+308 0.0d


324d d
char 16 0 to 65,535 '\u0000'
boolean 1 NA NA false
20
Datatypes
What will be the result, if we try to compile and
execute the following code?
class Test {
public static void main(String [ ] ar)
{
byte b=128;
Error: incompatible types: possible lossy
System.out.println(b); conversion from int to byte
} byte b=128;
1 error
}

21
Datatypes
What will be the result, if we try to compile and
execute the following code?
class Test {
public static void main(String ar[]) {
float f=1.2;
boolean b=1; error: incompatible types: possible lossy conversion
from double to float
System.out.println(f); float f=1.2;
System.out.println(b); error: incompatible types: int cannot be converted to
} boolean
boolean b=1;
}

22
Datatypes
class execute
What will be the result, if we try to compile and Test {
public static void main(String ar[]) {
the following code?
double d=1.2D;
class Test {
public static void main(String ar[]) { System.out.println(d);}
}
double d=1.2;
System.out.println(d);}
}
class Test {
public static void main(String ar[]) {
double d=1.2d; Output:1.2
System.out.println(d);}
}
23
Datatypes Conversion-Promotion

24
Datatypes Conversion-Casting

25
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

26
Keywords
What will be the result, if we try to compile and execute the
following code?
class Test {
public static void main(String [ ] ar)
{
Error
int for=128;
System.out.println(b);
}
}

27
Variables

28
Variables
A variable is a container which holds the
value. A variable is assigned with a data
type. Variable is a name of memory
location.

Ex: int num=10; 10 num

29
Variables
Variable Naming Conventions
 Begin with a alphabet or _ not with a digit
 Begin each variable with a lowercase letter
 Subsequent words should be capitalized:
 − myVariable
 Choose names that are mnemonic and that indicate the intent of
the variable to the casual observer
 Remember that …
 −Names are case-sensitive
 −Names can’t include white space

30
Input and Output

31
Output Methods
System.out.print():
• This method prints the text on the console and the cursor remains at
the end of the text at the console.
• The next printing takes place from just here.
• This method must take at least one parameter else it will throw an
error.

Java Java Java

32
Output Methods
System.out.println():
• This method prints the text on the console and the cursor remains at
the start of the next line at the console.
• The next printing takes place from the next line.
• This method may or may not take any parameter..

Java
Java
Java

33
Output Methods
System.out.printf():
• This method prints the text on the console and the cursor remains at the
start of the next line at the console.
• The next printing takes place from the next line.
• This method may or may not take any parameter.

Java is number 1 language

34
Getting Input from Keyboard
 The Scanner class in Java is located within the java.util library.
 To collect input from the keyboard in Java, there are several
methods available, with the java.util.Scanner being one of the
notable ones.
 Commonly utilized for breaking down text into strings and various
basic data types, the Java Scanner class leverages regular
expressions to accomplish this.
 Acquiring user input in Java is most straightforward when using
the Scanner class.
 Java's Scanner allows the acquisition of user data in fundamental
data types like integers, long integers, doubles, bytes, floats, shorts,
and more.
35
Getting Input from Keyboard

Step 1:Create Instance for Scanner class

Scanner in = new Scanner(System.in);

36
Getting Input from Keyboard
Step 2: To get Inputs of different datatypes
String next() It is used to get the next complete token from the scanner which is
in use.
BigDecimal nextBigDecimal() It scans the next token of the input as a BigDecimal.
BigInteger nextBigInteger() It scans the next token of the input as a BigInteger.
boolean nextBoolean() It scans the next token of the input into a boolean value and
returns that value.
byte nextByte() It scans the next token of the input as a byte.
double nextDouble() It scans the next token of the input as a double.
float nextFloat() It scans the next token of the input as a float.
int nextInt() It scans the next token of the input as an Int.
String nextLine() It is used to get the input string that was skipped of the Scanner
object.
long nextLong() It scans the next token of the input as a long.
37
Getting Input from Keyboard
Example

Enter your name:Sorna


Name is: Sorna

38
Thank you!
Write a closing statement or call-to-action here.

39

You might also like