java in easy ways

Download Java in Easy Ways

Post on 08-Apr-2018




0 download

Embed Size (px)


  • 8/6/2019 Java in Easy Ways



    Compilation : Randjithkumar, B.TechPage 1

    Java in Easy


  • 8/6/2019 Java in Easy Ways



    Compilation : Randjithkumar, B.TechPage 2



    Java is a simple and yet powerful object oriented programming language and it is in

    many respects similar to C++. Java was given birth at Sun Microsystems, Inc. in 1991. Java was

    conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at

    Sun Microsystems, Inc. It was developed to provide a platform-independent programming


    Java (with a capital J) is a high-level, third generation programming language, like C, Fortran,

    Smalltalk, Perl, and many others. You can use Java to write computer applications that crunch

    numbers, process words, play games, store data or do any of the thousands of other things

    computer software can do.

    Platform independent

    Unlike many other programming languages including C and C++ when Java is compiled, it is not

    compiled into platform specific machine, rather into platform independent byte code. This byte

    code is distributed over the web and interpreted by Java virtual Machine (JVM) on whichever

    platform it is being run.

    Compared to other programming languages, Java is most similar to C. However although Java

    shares much of C's syntax, it is not C. Knowing how to program in C or, better yet, C++, will

    certainly help you to learn Java more quickly, but you don't need to know C to learn Java. Unlike

    C++ Java is not a superset of C. A Java compiler won't compile C code, and most large C

    programs need to be changed substantially before they can become Java programs.

    Java Virtual Machine

    Java was designed with a concept of write once and run everywhere. Java Virtual Machine

    plays the central role in this concept. The Java Virtual Machine (JVM) is the environment in

    which Java programs execute. It is a software that is implemented on top of real hardware and

    operating system. When Java source code (.java files) is compiled, it is translated into Java

  • 8/6/2019 Java in Easy Ways



    Compilation : Randjithkumar, B.TechPage 3

    bytecodes and then placed into (.class) files. The JVM executes Java bytecodes. So Java

    bytecodes can be thought of as the machine language of the JVM. A Java virtual machine can

    either interpret the bytecode one instruction at a time or the bytecode can be compiled further for

    the real microprocessor using what is called a just-in-time compiler. The JVM must beimplemented on a particular platform before compiled Java programs can run on that platform.

    Java has powerful features. The following are some of them:-

    Java is object oriented

    Since Java is an object oriented programming language it has following advantages:

    y Reusability of Codey Emphasis on data rather than procedurey Data is hidden and cannot be accessed by external functionsy Objects can communicate with each other through functionsy New data and functions can be easily added

    Java is Distributed

    With extensive set of routines to handle TCP/IP protocols like HTTP and FTP java can open and

    access the objects across net via URLs.

    Java is Multithreaded

    One of the powerful aspects of the Java language is that it allows multiple threads of execution to

    run concurrently within the same program A single Java program can have many different

    threads executing independently and continuously. Multiple Java applets can run on the browser

    at the same time sharing the CPU time.

    Java is Secure

  • 8/6/2019 Java in Easy Ways



    Compilation : Randjithkumar, B.TechPage 4

    Java was designed to allow secure execution of code across network. To make Java secure many

    of the features of C and C++ were eliminated. Java does not use Pointers. Java programs cannot

    access arbitrary addresses in memory.

    Garbage collection

    Automatic garbage collection is another great feature of Java with which it prevents inadvertent

    corruption of memory. Similar to C++, Java has a new operator to allocate memory on the heap

    for a new object. But it does not use delete operator to free the memory as it is done in C++ to

    free the memory if the object is no longer needed. It is done automatically with garbage


    Application of Java

    Java has evolved from a simple language providing interactive dynamic content for web pages to

    a predominant enterprise-enabled programming language suitable for developing significant and

    critical applications. Today, Java is used for many types of applications including Web based

    applications, Financial applications, Gaming applications, embedded systems, Distributed

    enterprise applications, mobile applications, Image processors, desktop applications and many


    The most special about Java in relation to other programming languages is that it lets you write

    special programs called applets that can be downloaded from the Internet and played safely

    within a web browser. Traditional computer programs have far too much access to your system

    to be downloaded and executed willy-nilly. Although you generally trust the maintainers of

    various ftp archives and bulletin boards to do basic virus checking and not to post destructive

    software, a lot still slips through the cracks. Even more dangerous software would be

    promulgated if any web page you visited could run programs on your system.

    JAVA VS. C++

    Java C++

  • 8/6/2019 Java in Easy Ways



    Compilation : Randjithkumar, B.TechPage 5

    Method bodies must be defined inside the class

    to which they belong.

    Method bodies must be defined inside the class

    to which they belong.

    No forward referencing required. Explicit forward referencing required.

    No preprocessor. Heavy reliance on preprocessor.

    No comma operator. Comma operator.

    No variable-length parameter lists. Variable-length parameter lists.

    No optional method parameters. Optional function parameters.

    No const reference parameters. const reference parameters.

    No goto Goto

    Labels on break and continue. No labels on break and continue.

    Command-line arguments do not include the

    program name.

    Command-line arguments do not include the

    program name.

    Main method cannot return a value. Main function can return a value.

    No global variables. Global variables.

    Character escape sequences can appear in a


    Character escape sequences must appear in astring

    or character literal.

    Cannot mask identifiers through scope. Can mask identifiers through scope.


    Data type defines a set of permitted values on which the legal operations can beperformed. In java, all the variables needs to be declared first i.e. before using a particular

    variable, it must be declared in the program for the memory allocation process. Like

    int pedal = 1;

    This statement exists a field named "pedal" that holds the numerical value as 1. The value

    assigned to a variable determines its data type, on which the legal operations of java are performed. This behavior specifies that, Java is a strongly-typed programming language.

    The data types in the Java programming language are divided into two categories and can be

    explained using the following hierarchy structure :

  • 8/6/2019 Java in Easy Ways



    Compilation : Randjithkumar, B.TechPage 6

    Primitive Data Types

    The primitive data types are predefined data types, which always hold the value of the same

    data type, and the values of a primitive data type don't share the state with other primitive

    values. These data types are named by a reserved keyword in Java programming language.

    There are eight primitive data types supported by Java programming language :

    byteThe byte data type is an 8-bit signed two's complement integer. It ranges from -128 to127(inclusive). This type of data type is useful to save memory in large arrays.. We can also use

    byte instead ofint to increase the limit of the code. The syntax of declaring a byte type variableis shown as:

    byte b = 5;

    shortThe short data type is a 16-bit signed two's complement integer. It ranges from -32,768 to

    32,767. short is used to save memory in large arrays. The syntax of declaring a short typevariable is shown as:

    short s = 2;

    intThe int data type is used to store the integer values not the fraction values. It is a 32-bit signed

    two's complement integer data type. It ranges from -2,147,483,648 to 2,147,483,647 that is more

    enough to store large number in your program. However for wider range of values uselong. Thesyntax of declaring a int type variable is shown as:

    int num =50;

    longThe long data type is a 64-bit signed two's complement integer. It ranges from -

  • 8/6/2019 Java in Easy Ways



    Compilation : Randjithkumar, B.TechPage 7

    9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Use this data type with larger rangeof values. The syntax of declaring a long type variable is shown as:

    long ln = 746;

    floatThe float data type is a single-precision 32-bit