Skip to main content

Java

Java Tutorials Welcome to Java Tutorials

This Java tutorials is seperated into following sections:
Introduction to Java
C++ vs Java
Features of Java
Java Virtual Machine (JVM)
Java’s Magic Byte Code
Java Runtime Environment (JRE)
Input/Output in Java
Basic Language Elements in Java
Java Identifiers
Java Comments
Java Data Types
Java Variables
Java Operators
Java Loops
Class and Object
Constructors
Nested Class and Inner Class
Static Member
Array
String

Java

What is Java?

  • Java is a software platform and computer programming language.
  • Java is commonly used to refer to the Java platform, a set of tools allowing for easy cross-platform application development, as well as the Java programming language, which is a general-purpose programming language often used to develop programs for this platform.
  • Code written in Java can be run on any system that a Java virtual machine (JVM) can run on. This concept of write once, run anywhere was used as a slogan to promote Java’s cross-platform abilities.
  • Java environments can be found on all sorts of devices, large and small.
  • Be careful not to confuse Java with JavaScript. While both languages are now found in numerous environments, JavaScript, which is most commonly used to power interactivity inside of a web browser, is a different tool completely. Other than a part of the name, the two don’t share much in common.

C++ vs Java

Comparison Index C++ Java
Platform-independent C++ is platform-dependentJava is platform-independent
Multiple inheritanceC++ supports multiple inheritanceJava doesn't support multiple inheritance through class. It can be achieved by interfaces in java.
Operator OverloadingC++ supports operator overloadingJava doesn't support operator overloading
PointersC++ supports pointersJava do not supports pointer
Structure and UnionC++ supports structures and unionsJava doesn't support structures and unions

Features of Java

  • Simple: Java has a concise, cohesive set of features that makes it easy to learn and use. Java was designed to be easy for the professional programmer to learn and use effectively. Assuming that you have some programming experience, you will not find Java hard to master. If you already understand the basic concepts of object-oriented programming, learning Java will be even easier. Best of all, if you are an experienced C++ programmer, moving to Java will require very little effort. Because Java inherits the C/C++ syntax and many of the object-oriented features of C++
  • Secure: Java provides a secure means of creating Internet applications. As you are likely aware, every time that you download a “normal” program, you are risking a viral infection. When you use a Java-compatible Web browser, you can safely download Java applets without fear of viral infection or malicious intent. Java achieves this protection by confining a Java program to the Java execution environment and not allowing it access to other parts of the computer.
  • Portable: Java programs can execute in any environment for which there is a Java runtime system. Many types of computers and operating systems are in use throughout the world—and many are connected to the Internet. For programs to be dynamically downloaded to all the various types of platforms connected to the Internet, some means of generating portable executable code is needed.
  • Object-oriented: Java embodies the modern, object-oriented programming philosophy. Like C++, Java can support an object-oriented approach to writing software. Ideally, object-oriented design can permit the creation of software components that can be reused. Object-oriented programming is based upon modeling the world in terms of software components called objects. An object consists of data and operations that can be performed on that data called methods. These methods can encapsulate, or protect, an object’s data because programmers can create objects in which the methods are the only way to change the state of the data.
  • Robust: Java encourages error-free programming by being strictly typed and performing run-time checks. The ability to create robust programs was given a high priority in the design of Java. To gain reliability, Java restricts you in a few key areas, to force you to find your mistakes early in program development. At the same time, Java frees you from having to worry about many of the most common causes of programming errors. Because Java is a strictly typed language, it checks your code at compile time. However, it also checks your code at run time
  • Multithreaded: Java provides integrated support for multithreaded programming. Java was designed to meet the real-world requirement of creating interactive, networked programs. To accomplish this, Java supports multithreaded programming, which allows you to write programs that do many things simultaneously.
  • Architecture-neutral: Java is not tied to a specific machine or operating system architecture. One of the main problems facing programmers is that no guarantee exists that if you write a program today, it will run tomorrow even on the same machine. Operating system upgrades, processor upgrades, and changes in core system resources can all combine to make a program malfunction.
  • Interpreted: Java supports cross-platform code through the use of Java bytecode. Java enables the creation of cross-platform programs by compiling into an intermediate representation called Java bytecode. This code can be interpreted on any system that provides a Java Virtual Machine.
  • High performance: The Java bytecode is highly optimized for speed of execution. The Java bytecode was carefully designed so that it would be easy to translate directly into native machine code for very high performance by using a just-in-time compiler.
  • Distributed: Java was designed with the distributed environment of the Internet in mind. Java is designed for the distributed environment of the Internet, because it handles TCP/IP protocols. In fact, accessing a resource using a URL is not much different from accessing a file. The original version of Java (Oak) included features for intra-addressspace messaging. This allowed objects on two different computers to execute procedures remotely. Java revived these interfaces in a package called Remote Method Invocation.
  • Dynamic: Java programs carry with them substantial amounts of run-time type information that is used to verify and resolve accesses to objects at run time. Java programs carry with them substantial amounts of run-time type information that is used to verify and resolve accesses to objects at run time. This makes it possible to dynamically link code in a safe and expedient manner.

Java Virtual Machine (JVM)

  • Java is an architectural neutral language.
  • Write once execute anywhere approach.
  • Java compiler does not translate the source code into the machine language.
  • Compiler translates source code into an intermediate code (bytecode) which is not understood by operating system.
  • Process of converting source code into bytecode.
  • JVM convert this bytecode into machine code understood by the operating system.
  • JVM exists only inside the computer memory.
  • It is a simulated computer within the computer.
  • It can perform all major functions of a real computer.
  • Process of converting source code in machine code.

Java’s Magic Byte Code

  • It allows Java to solve both the security and the portability problems is that the output of a Java compiler is not executable code but it is the Bytecode.
  • Bytecode is a highly optimized set of instructions designed to be executed by the Java run-time system, which is called the Java Virtual Machine (JVM).
  • JVM is an interpreter for bytecode.
  • Java program is executed by JVM helps solve the major problems associated with downloading programs over the Internet.
  • Translating a Java program into bytecode helps makes it much easier to run a program in a wide variety of environments.
  • Once the run-time package exists for a given system, any Java program can run on it.
  • The use of byte code enables the Java run-time system to execute programs much faster.

Java Runtime Environment (JRE)

  • Java Runtime Environment (JRE) is a virtual environment that contains software tools and libraries that support the execution of a Java program.
  • Without JRE a Java program or Java application cannot run in a system.
  • JRE is responsible to load Java programs and applications into the JVM for execution.
  • It also provides necessary libraries and set of tools that are required for the execution.

Input/Output in Java

  • It is used to process the input and produce the output.
  • Java uses the concept of a stream to make I/O operation fast.
  • The java.io package contains all the classes required for input and output operations.
  • System.in: This is the standard input stream that is used to read characters from the keyboard or any other standard input device.
  • System.out: This is the standard output stream that is used to produce the result of a program on an output device like the computer screen.
  • print():
    • It is used to display a text on the console.
    • Text is passed as the parameter to this method in the form of String.
    • It 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.
    • Syntax: System.out.print(parameter);
  • println():
    • It is also used to display a text on the console.
    • It prints the text on the console and the cursor moves to the start of the next line at the console.
    • The next printing takes place from the next line.
    • Syntax: System.out.println(parameter);
  • printf():
    • This is the easiest of all methods as this is similar to printf in C.
    • Note that System.out.print() and System.out.println() take a single argument, but printf() may take multiple arguments.
    • This is used to format the output in Java.

Basic Language Elements in Java

  • Case Sensitivity
  • Class Names: first letter should be in Upper Case. If several words are used to form a name of the class, each inner word's first letter should be in Upper Case.
    Example: class MyFirstJavaClass
  • Method Names: should start with a Lower Case letter. If several words are used to form the name of the method, then each inner word's first letter should be in Upper Case.
    Example: public void myMethodName()
  • Program File Name: Name of the program file should exactly match the class name.
  • When saving the file, you should save it using the class name (Remember Java is case sensitive) and append '.java' to the end of the name.
  • Example: Assume 'MyFirstJavaProgram' is the class name. Then the file should be saved as 'MyFirstJavaProgram.java'
  • public static void main(String args[ ]): Java program processing starts from the main() method which is a mandatory part of every Java program.

Java Identifiers

  • All Java components require names. Names used for classes, variables, and methods are called identifiers.
  • All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_).
  • After the first character, identifiers can have any combination of characters.
  • A key word cannot be used as an identifier.
  • Most importantly, identifiers are case sensitive.
  • Example:
    • legal identifiers: age, $salary, _value, __1_value.
    • illegal identifiers: 123abc, -salary.

Java Comments

  • In Java there are two types of comments:
    • Single-line comments
      Example:
      //Comments here( Text in this line only is considered as comment )
    • Multi-line comments
      Example: /*Comment start continues continues . . . Comment ends*/

Java Data Types


Java Variables

  • A variable is a name given to a memory location. It is the basic unit of storage in a program.
  • The value stored in a variable can be changed during program execution.
  • A variable is only a name given to a memory location, all the operations done on the variable effects that memory location.
  • In Java, all the variables must be declared before use.

Java Operators

  • Arithmetic Operators
  • Unary Operators
  • Assignment Operator
  • Relational Operators
  • Logical Operators
  • Ternary Operator
  • Bitwise Operators
  • Shift Operators

Java Decision Statements

  • if
  • if-else
  • nested-if
  • if-else-if
  • switch-case
  • jump

Java Loops

  • while loop
  • for loop
  • do while

Class and Object

  • In Java, Data items are called fields
  • Functions are called methods.
  • Variables are termed as instance of classes.
  • Defining a Class:
    • Class is a user defined data type with a template that serves to define its properties.
    • Syntax:
      class classname [extends superclassname]
      {
      [fields declaration]
      [method declaration]
      }
      
    • Everything inside the square brackets is optional.
    • No semicolon after closing brace.
  • Field Declaration:
    • Data is encapsulated in a class by placing data fields inside the body of the class definition.
    • Example:
      class Rectangle
      {
      int length, width; or int length;
                             int width; 
      }
      
  • Method Declaration:
    • Method declaration have four basic parts:
      • Name of the method
      • Type of the value the method returns
      • List of parameters
      • Body of the method.
    • General Syntax:
      type method name (parameter-list)
      {
      method-body;
      }
      
  • Creating Object:
    • An entity that has state and behavior is known as an object.
    • State: represents the data (value) of an object.
    • Behavior: represents the behavior (functionality) of an object.
    • Example:
      Rectangle rect1;
      Rect1= new Rectangle();
      Or
      Rectangle rect1 = new Rectangle();
           

Constructors

  • Enables an object to initialize itself when it is created.
  • Constructors have the same name as the class itself.
  • Do not specify a return type.
  • They return the instance of class itself.
  • Example
    Class Rectangle {
    int length;
    int width;
    Rectangle(int x , int y)
    { length=x;
      width=y; }
    int rectArea()
    { return(length * width) }
    }
    

Nested Class and Inner Class

  • We can write a class within another class in java.
  • The class written within is called nested class.
  • The class that holds the inner class is called the outer class.
  • Syntax:
    Class Outer_Demo
     {
     Class Nested_Demo
       {
       }
     }
    
  • In java we can not define any class private but we can do it by using nested class concept.
  • Inner class can called the members of outer class (private or public).
  • Inner classes are a security mechanism in java.
  • Inner class can be private and once you declare an inner class private, it cannot be accessed from an object outside the class.

Static Member

  • Class contains two sections:-variable and method.
  • These variables and methods are called instance variable and instance method.
  • Syntax:
    Static int count;
    or
    Static int max(int x, int y);
    
  • Static variables are used when we want to have a variable common to all instances of a class.
  • Static variables, static methods can be called without using the objects.
  • They are also available for use by other classes.

Array

  • Array stores a fixed-size sequential collection of elements of the same type.
  • An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
  • Why Array?
    Implement an application that will calculate 100 students exam average.
    Variables needed?
    int studentA;
    int studentB;
    int studentC;
    int studentD;
    ……..
    ……..
    
  • Declaration of array
    int[ ] mark;
    byte[ ] age;
    double[ ] height;
    Or
    int mark[ ];
    byte age[ ];
    double height[ ];
    
  • Array declaration in C++:
    int myarray[10];
  • Array declaration in Java:
    int [ ] myarray;
    myarray =new int[10];
    
  • Storage for the array itself is not allocated until you use “new”.
    For initializing method the “new” command is not needed.
    int [ ] myarray={5,3,7,89,2};
  • Initialization of array
    Initialization is loading the array with the values.
    Int[ ] myarray =new int[5]
    myarray[0]=32;
    myarray[1]=12;
    myarray[2]=66;
    myarray[3]=54;
    myarray[4]=43;
    
  • Two Dimensional Array
    Int[ ][ ] myarray = new int[2][3];
    myarray[0][0]=32;
    myarray[0][1]=12;
    myarray[0][2]=66;
    myarray[1][0]=54;
    myarray[1][1]=43;
    myarray[1][2]=36;
    
  • Multi Dimensional Array
      Int[ ][ ][ ] myarray=new int[2][3][2];
    

String

  • A sequence of characters.
  • Each character is a Unicode character.
  • Represented by the String data type in Java.
  • Example:
    String s = "Hello, Java";
    
  • String objects contain an immutable (read-only) sequence of characters.
  • Use Unicode in order to support multiple languages and alphabets.
  • Stores strings in the dynamic memory (managed heap).
  • String Class
    • String class is present in java.lang package.
    • String class provides a lot of methods for performing the operations on the strings.
    • we can create String object using two ways:
      • By new keyword
      • By using String literal.
    • If we create String Object through the new keyword.
    • JVM will create String object into the Heap memory.
    • String str1=new String("Good Morning");
      
    • If we create String object using String literal, JVM will store it into the String constant pool.
      String str2="Good morning";
      
  • Constant Pool
    • String constant pool is a special memory area which is a part of Heap memory.
    • When we create String object using String literal, JVM first checks if String is already available in the String constant pool.
    • If it exists, JVM will create only reference to it.
    • If it does not exist, JVM will create new String object and store it in String constant pool.

Other Tutorials
HTML Tutorials
Java Tutorials
RDBMS Tutorials
Other Popular Posts
HTML
Information System
Cloud Computing
Mobile Computing
E-Commerce
Computer
Java
Databse
Operating System

Popular posts from this blog

HTML

What is HTML? HTML stands for Hyper Text Markup Language. HTML is not a programming language. HTML is a standard markup language. HTML is basic building block of web. HTML is a language used for describing web pages and creating websites. Other Tutorials HTML Tutorials Java Tutorials RDBMS Tutorials Other Popular Posts HTML Information System Cloud Computing Mobile Computing E-Commerce Computer Java Databse Operating System

Java

What is Java? Java is a software platform and computer programming language. Java is commonly used to refer to the Java platform, a set of tools allowing for easy cross-platform application development, as well as the Java programming language, which is a general-purpose programming language often used to develop programs for this platform. Code written in Java can be run on any system that a Java virtual machine (JVM) can run on. This concept of write once, run anywhere was used as a slogan to promote Java’s cross-platform abilities. Java environments can be found on all sorts of devices, large and small. Be careful not to confuse Java with JavaScript. While both languages are now found in numerous environments, JavaScript, which is most commonly used to power interactivity inside of a web browser, is a different tool completely. Other than a part of the name, the two don’t share much in common. Other Tutorials HTML Tutorials Java

Information System

What is Information System? An information system (IS) can be any organized combination of people, hardware, software, communications networks, data resources, and policies and procedures that stores, retrieves, transforms, and disseminates information in an organization. Examples of Information Systems Smoke signals for communication were used as early as recorded history and can account for the human discovery of fire. Card catalogs in a library are designed to store data about the books in an organized manner that allows readers to locate a particular book by its title, author name, subject, or a variety of other approaches. The Fundamental Roles of IS in Business Support of business processes and operations. Support of decision making by employees and managers. Support of strategies for competitive advantage. Other Tutorials HTML Tutorials Java Tutorials RDBMS Tutorials Other Popular Posts HTML Informa