java basic

31
JAVA Fundamental of java Prepared by Miss. Arati A. Gadgil

Upload: arati-gadgil

Post on 11-Apr-2017

287 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Java basic

JAVA Fundamental of

java

Prepared by

Miss. Arati A. Gadgil

Page 2: Java basic

2

Java

Java was developed in the early 90s by Sun Microsystems

Java is a high-level language.

There are many features of java. They are also known as java buzzwords.

Simple

Java omits many rarely used, poorly understood, confusing features of C++. Say : No Pointer! No dynamic delete.

Page 3: Java basic

3

Object Oriented

Object –oriented design is a technology that focuses design on the data (object) and on the interfaces to it.

Everything is an object, everything will become a class in Java. Every java program, in top- level view, is classes.

Robust

The single biggest difference between Java and C/C++ is that Java has “a inner safe pointer-model”, therefore it eliminates the possibility of overwriting memory and corrupting data, so programmers feel very safe in coding.

Page 4: Java basic

4

Distributed

Basically, Java is for Net-Work application, for WEB project. Java can open and access “objects” across the Net via URLs (Uniform Resource Locator)eg. “http//:gamut.neiu.edu/~ylei/home.html”, with the same ease as when accessing a local file system

Platform Independent

Java code can be run on multiple platforms e.g. Windows, Linux, Sun Solaris,Mac/OS etc. Java code is compiled by the compiler and converted into bytecode.This bytecode is a platform independent code because it can be run on multiple platforms i.e. Write Once and Run Anywhere(WORA).

Page 5: Java basic

5

Secured

Java is secured because:No explicit pointer,Programs run inside virtual machine sandbox also,•Classloader- adds security by separating the package for the classes of the local file system from those that are imported from network sources.

•Bytecode Verifier- checks the code fragments for illegal code that can violate access right to objects.

•Security Manager- determines what resources a class can access such as reading and writing to the local disk

Page 6: Java basic

6

Architecture-neutral

The compiler generates an architecture-neutral object file format. The compile code can run on many processors ,given the presence of the runtime system. The java compiler does this by generting bytecode instruction which have nothing to do with a particular computer architecture.

PortableWe may carry the java bytecode to any platform.

High-performance

Java is faster than traditional interpretation since byte code is "close" to native code still somewhat slower than a compiled language (e.g., C++)

Page 7: Java basic

7

Multi-threaded

A thread is like a separate program, executing concurrently. We can write Java programs that deal with many tasks at once by defining multiple threads. The main advantage of multi-threading is that it shares the same memory. Threads are important for multi-media, Web applications etc.

Dynamic

In number of ways, java is more dynamic language than C or C++.It was designed to adapt to an evolving environment. Libraries can freely add new methods and instance variables without any effect on their clients. In java finding out runtime type information is straightforward.

Page 8: Java basic

8

Java Virtual Machine

The .class files generated by the compiler are not executable binaries, Java combines compilation and interpretation.

Instead, they contain “byte-codes” to be executed by the Java Virtual Machine.

This approach provides platform independence, and greater security.

The JVM performs following main tasks:•Loads code•Verifies code•Executes code•Provides runtime environment

Page 9: Java basic

9

JRE

JRE is an acronym for Java Runtime Environment. It is used to provide runtime environment.It is the implementation of JVM. It physically exists.It contains set of libraries + other files that JVM uses at runtime.

JDK

JDK is an acronym for Java Development Kit. It physically exists. It contains JRE + development tools

Page 10: Java basic

10

JVM

Java Virtual Machine (JVM) is an abstract computing machine. Java Runtime Environment (JRE) is an implementation of the JVM. Java Development Kit (JDK) contains JRE along with various development tools like Java libraries, Java source compilers, Java debuggers, bundling and deployment tools.

Page 11: Java basic

11

Relations between JVM JRE JDK

Page 12: Java basic

12

JIT

JIT refers to execution engine in few of JVM implementations, one that is faster but requires more memory, is a just-in-time compiler. In this scheme, the bytecodes of a method are compiled to native machine code the first time the method is invoked.

JIT refers to execution engine in few of JVM implementations, one that is faster but requires more memory,is a just-in-time compiler. In this scheme, the bytecodes of a method are compiled to native machine code the first time the method is invoked.

Page 13: Java basic

13

Java Bytecode

Java bytecode is the instruction set of the Java virtual machine. Eachbytecode is composed by one, or in some cases two, bytes that represent the instruction (opcode), along with zero or more bytes for passing parameters..

Bytecode is nothing but the intermediate representation of Java source code which is produced by the Java compiler by compiling that source code. This byte code is an machine independent code.It is not an completely a compiled code but it is an intermediate code somewhere in the middle which is later interpreted and executed by JVM

Page 14: Java basic

14

HotSpot, released as the "Java HotSpot Performance Engine"is a Java virtual machine for desktops and servers, maintained and distributed by Oracle Corporation. It features techniques such as just-in-time compilation and adaptive optimization designed to improve performance.

Sun intended to write a new just-in-time (JIT) compiler for the newly developed virtual machine.This new compiler would give rise to the name "HotSpot", which derives from the fact that, as the software runs Java bytecode, it continually analyzes the program's performance for "hot spots" which are frequently or repeatedly executed. These are then targeted for optimization, leading to high-performance execution with a minimum of overhead for less performance-critical code

Java HotSpot

Page 15: Java basic

15

Sun's JRE features two virtual machines, one called Client and the other Server. The Client version is tuned for quick loading. It makes use of interpretation. The Server version loads more slowly, putting more effort into producing highly optimized JI compilations, that yield higher performance. Both VMs compile only often-run methods, using a configurable invocation-count threshold to decide which methods to compile.

The HotSpot Java virtual machine is written in C++. As stated on the HotSpot web page, the source contains approximately 250,000 lines of code.

 Hotspot provides:•A class loader•A bytecode interpreter•Client and Server virtual machines, optimized for their respective uses•Several garbage collectors•A set of supporting runtime libraries

Page 16: Java basic

16

The main() method

public static void main(String args[]){

... }public--- the interpreter can call it.

static ----It is a static method belonging to the class.

void -----It does not return a value.

String----It always has an array of String objects as its formal parameter. the array contains any arguments passed to the program on the command line.The source file’s name must match the class name which main method is in.

Page 17: Java basic

17

Source File Declaration Rules

There can be only one public class per source code file.Comments can appear at the beginning or end of any line in the source code file; they are independent of any of the positioning rules discussed here.

If there is a public class in a file, the name of the file must match the name of the public class.

For example, a class declared as public class Dog { }must be in a source code file named Dog.java.

If the class is part of a package, the package statement must be the first line in the source code file, before any import statements that may be present.

Page 18: Java basic

18

If there are import statements, they must go between the package statement (if there is one) and the class declaration. If there isn't a package statement, then the import statement(s) must be the first line(s) in the source code file.

If there are no package or import statements, the class declaration must be the first line in the source code file.

import and package statements apply to all classes within a source code file. In other words, there's no way to declare multiple classes in a file and have them in different packages, or use different imports.

A file can have more than one nonpublic class.Files with no public classes can have a name that does not match any of the classes in the file

Page 19: Java basic

19

Comments

/* This kind of comment can span multiple lines */

// This kind is to the end of the line

/** * This kind of comment is a special * ‘javadoc’ style comment */

Page 20: Java basic

20

Primitive types

•int 4 bytes•short 2 bytes•long 8 bytes•byte 1 byte•float 4 bytes•double 8 bytes•char Unicode encoding (2 bytes) •boolean {true,false}

Behaviors is exactly as in C++

Page 21: Java basic

21

• Constants 37 integer 37.2 float 42F float 0754 integer (octal) 0xfe integer (hexadecimal)

• Comparison operators

== equal!= not equal< less than> greater than<= less than or equal>= greater than or equal

Page 22: Java basic

22

Variable declaration

type variable-name;

Meaning: variable <variable-name> will be a variable of type <type>

Where type can be:

int //integerdouble //real number

Example: int a, b, c; double x;int sum;

Page 23: Java basic

23

String is an Object

Constant strings as in C, does not exist

The function call show(“Hello”) creates a String object, containing “Hello”, and passes reference to it to show.

The String object is a constant. It can’t be changed using a reference to

it.

Page 24: Java basic

24

Writing to user (output)

System.out.println(variable-name);prints the value of variable <variable-name> to the user

System.out.println(“any message “);prints the message within quotes to the user

System.out.println(“hello” + “world” + a + “plus“ + b);assuming the value of a is 3 and of b is 7, it printshelloworld3plus7

Note: System.out.println() always prints on a new line.

Page 25: Java basic

25

Example

/* This program illustrates the System.out.println command.*/public class Hello {

public static void main (String args[]) {

System.out.println(“This is my first Java program!”);System.out.print(“I like Java.”); System.out.print(“I think Java is cool.”);

} // end of main} // end of class

Page 26: Java basic

26

Example/* Printing ages.*/public class MyFirstJavaProgram {

public static void main (String args[]) {

int myAge, myFriendAge; /* declare two integer variables */

myAge = 20; myFriendAge = myAge + 1; //one year older System.out.println(“Hello, I am “ +myAge + “years old, and my friend is “ + myFriendAge + “ years old”);System.out.println(“Goodbye”);

} // end of main} // end of class

Page 27: Java basic

27

Flow control

if/else

do/while

for

switch

If(x==4) { // act1} else { // act2}

int i=5;do { // act1 i--;} while(i!=0);

int j;for(int i=0;i<=9;i++) { j+=i;}

char c=IN.getChar();switch(c) { case ‘a’: case ‘b’: // act1 break; default: // act2}

Page 28: Java basic

28

Big Numbers

If the precision of the basic integer and floating-point types is not sufficient, you can turn to a couple of handy classes in the java.math package, called BigInteger and BigDecimal. These are classes for manipulating numbers with an arbitrarily long sequence of digits. The BigInteger class implements arbitrary precision integer arithmetic, and BigDecimal does the same for floating-point numbers.Use the static valueOf method to turn an ordinary number into a big number:

BigInteger a = BigInteger.valueOf(100);

BigInteger c = a.add(b); // c = a + b BigInteger d = c.multiply(b.add(BigInteger.valueOf(2)));

// d = c * (b + 2)

Page 29: Java basic

29

Array

•Array is an object

• Array size is fixed

A[] arr; // nothing yet …

arr = new A[4]; // only array of pointers

for(int i=0 ; i < arr.length ; i++) arr[i] = new Animal();

Page 30: Java basic

30

Arrays - Multidimensional

• In C++

Animal arr[2][2]

Is:

• In Java

What is the type of the object here ?

A[][] arr= new A[2][2]

Page 31: Java basic

Thank You

31