Java Tutorials

Escape Sequences in Java

 

Objectives :

  • What are the escape sequences in Java?
  • What are the escape characters in Java?

 

Generally we enter all the characters directly. The escape sequences are used for those characters which cannot be entered directly.

Sequence Purpose
\n New Line (i.e. Line feed)
\b Backspace
\t Tab
\r Carriage return
\f Form Feed
\\ Backslash
\’ Single quote
\” Double quote
\ddd Octal Characters (i.e. ddd)
\uxxxx Hexadecimal Unicode Characters (i.e. xxxx)

Escape Sequences in Java

Separators in Java

Following are the some characters which are generally used as the separators in Java.

Separator

Name

Use

.

Period

It is used to separate the package name from sub-package name & class name. It is also used to separate variable or method from its object or instance.

,

Comma

It is used to separate the consecutive parameters in the method definition. It is also used to separate the consecutive variables of same type while declaration.

;

Semicolon

It is used to terminate the statement in Java.

()

Parenthesis

This holds the list of parameters in method definition. Also used in control statements & type casting.

{}

Braces

This is used to define the block/scope of code, class, methods.

[]

Brackets

It is used in array declaration.

Separators in Java

 

Operators in Java

In Java we are provided with number of different operators & these operators are further divided into arithmetic, bitwise, relational and logical groups.

Arithmetic Operators

The arithmetic operators are used to perform the arithmetic operations in algebra.

Operator Operation
+ Addition
Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
+= Addition assignment
-= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
Decrement

Arithmetic Operators in Java

Bitwise Operators

The bitwise operators are used to perform the different operations depending upon the individual operators.

Operator Operation
~ Bitwise Unary NOT
& Bitwise AND
| Bitwise OR
^ Bitwise Exclusive OR
>>  Shift Right with Sign Fill
>>>  Shift Right with Zero Fill
<<  Shift Left
&= Bitwise AND assignment
|= Bitwise OR assignment
^= Bitwise Exclusive OR assignment
>>= Shift Right assignment
>>>= Shift Right Zero Fill assignment
<<= Shift Left assignment

Bitwise Operators in Java

Relational Operators

The relational operators are used relate or to determine the relationship between the two operands.

Operator

Operation
== Equal to
!= Not equal to
Greater than
Less than
>= Greater than equal to
<= Less than equal to

Relational Operators in Java


Boolean Logical Operators

The boolean logical operators are used combine the two boolean operands resulting in new boolean value.

Operator Operation
& Logical AND
| Logical OR
^ Logical Exclusive OR
&& Short-circuit AND
|| Short-circuit OR
! Logical Unary NOT
&= AND assignment
|= OR assignment
^= Exclusive OR assignment
== Equal to
!= Not equal to
?: Ternary If-Then-Else

Boolean Logical Operators in Java

 

 The ? Operator

This is the special ternary operator in Java which is used to replace certain types of if-then-else statements.

Syntax :-

expression1 ? expression2 : expression3

 If expression1 results boolean value true then, expression2 is executed otherwise expression3 is executed.

Keywords in Java

In Java there are total 49 keywords are available.

The keywords “const” & “goto” are reserved & not used.

The “true”, “false” & “null” are values.

One cannot use the keywords for any other purpose.

Following are the keywords in Java:

abstract assert boolean break byte
case catch char class const
continue default do double else
extends final finally float for
goto if implements import instanceof
int interface long native new
package private protected public return
short static strictfp super switch
synchronized this throw throws transient
try void volatile while ——

Keywords in Java

Features of Java

Objective :-

  • What are the features of Java?
  • What are the key features of Java?
  • Which features are supported by Java?
  • What Object Oriented Features (OOPs features) of Java?

Following are the key features of the Java Programming Language :-

  1. Platform Independent
  2. Simple
  3. Object Oriented
  4. Robust
  5. Distributed
  6. Portable
  7. Secure
  8. Multithreaded
  9. Compiled & Interpreted
  10. Automatic Garbage Collection
  11. Built in Networking
  12. Dynamic Operations
  13. High Performance
  14. Free From
  15. Free Editor
  16. Case Sensitive

Platform Independent :

The concept of Java is “Write Once, Run Anywhere”. Here the term platform refers to the operating system, to run any java program we need JVM (Java Virtual Machine) and is not dependent on any of the tools & components provided by the operating system, therefore Java is platform independent.

Simple :

It is simple to understand i.e. user can write & update the code easily since many syntaxes are similar to C & C++, hence they are familiar too. Java also eliminates the several poor programming features of C & C++ like goto statements, header files, structures, operator overloading, multiple inheritance and pointers.

Object Oriented :

Java is based on the concepts on Object Oriented Programming Language such as Inheritance, Encapsulation, Polymorphism, Dynamic Binding, etc.

Robust :

The Java is strongly typed & allows the Java Compiler checks the program whether there any error and the Java Interpreter checks any run time error and makes the system secure from crash. These features make the java language robust.

Distributed :

Java supports different network connectivity such as HTTP, & FTP. They are also built in Java.

Portable :

The java language portable provided that the system must have interpreter for the JVM. Java also have the standard data size irrespective of operating system or the processor.

Secure :

Java does not use memory pointers explicitly. Programs run inside the virtual machine sandbox. Bytecode verification process in Java to ensure that the code loaded over the network does not violate Java security constraints such as;

  • No memory pointers
  • Array index limits
  • Access restrictions, etc.

Multithreaded :

Java supports multithreading. Thread is nothing but the Lightweight process. When two or more than two threads runs concurrently, then they are said to be Multithreaded. Java also support synchronization of threads.

Compiled & Interpreted :

Java requires two steps to run a program;

  1. Compilation &
  2. Interpretation

The Java Compiler (javac) compiles the java code & produces the bytecode. The file generated after successful compilation containing the bytecode is known as Class file (“.class”) or Byte Code File.

The generated class file is then interpreted by the Java Interpreter (java) which gives the output.

Automatic Garbage Collection :

Java supports automatic memory management, it is done by JVM.

Built in Networking :

Java support networking as of it was designed keeping networking in mind & has many classes which help to create network & internet applications very easily.

Dynamic Operations :

Java supports various dynamic operations on demand such as;

  • Linking of files from all over the world
  • Loading of new classes & libraries
  • Compilation of classes & libraries, etc.

High Performance :

Java uses native code & “just-in-time compiler” which converts the bytecode to machine code & since java is interpreted it gives high performance.

Free Form :

It is a free form language i.e. we can write multiple statements in a single line.

Free Editor :

It is free editor language i.e. we can edit Java programs in Notepad, Text pad, Dos editor, WordPad, VI editor, etc.

Case Sensitive :

Java is a case sensitive language i.e. it differs in small letters & capital letters.

All the above are the main features of the Java programming language.

Java Vs. C++

Objectives :

  • What are the differences between Java & C++?
  • What are the differences between C++ & Java?
  • Java Vs. C++

Java

C++

1. Java imports class files. 1. C++ includes header files.
2. Java doesn’t support “goto” statement. 2. C++ supports “goto” statement.
3. Java allows multi-level break & continue statements. 3. C++ doesn’t allows multi-level break & continue statements.
4. Java doesn’t support multiple inheritances but provides interfaces. 4. C++ supports multiple inheritances.
5. Java does not have destructors. 5. C++ has destructors.
6. Java supports auto garbage collection. 6. C++ does not supports auto garbage collection, requires memory management.
7. Java does not provide the facility of operator overloading. 7. C++ supports operator overloading.
8. Java neither supports RTI nor namespaces. 8. C++ supports RTI & namespaces.
9. Virtual (Dynamic dispatch) method is default. 9. Virtual method is needed to be implemented explicitly.
10. It is a platform independent language. 10. It is not a platform independent language.
11. “.class” file is generated after compilation. 11. “.exe” file is generated after compilation.
12. Java doesn’t support pointers. 12. C++ supports pointers.
13. Java doesn’t support Templates. 13. C++ supports Templates.
14. Java uses compiler & interpreter both. 14. C++ uses compiler only.
15. Java is far from hardware than C++. 15. C++ is more nearer to hardware than Java.
16. Java has primitive data type boolean. 16. In C++, boolean is not available.
17. Through Java we can develop Distributed / Internet based applications. 17. Development of Distributed / Internet based applications is not possible in C++.
18. Java can only pass by reference (well except primitives) 18. C++ can pass by value (default) or by reference.
19. Java posses 49 keywords including some reserved words. 19. C++ has 48 keywords. ANSI C++ has 15 more keywords.

 

Introduction to Java

Java is a object oriented programming language developed by a group of 6 people who were real hardcore programmers in Aspen, their names are as follows;

  1. Bill Joy,
  2. Andy Bechtolsheim,
  3. Wayne Rosing,
  4. Mike Sheridan,
  5. James Gosling and
  6. Patrick Naughton

The development was started under the name “Stealth Project”. In 1991, they launched “Oak” – name after tree outside Gosling’s window. It took nearly 18 months to build the first version of Oak. But in 1992, due to massive amount of hacking it fails. In Summer 1995, Sun Microsystems formerly launched it again with the name “Java” – named after a hot drink like tea.

The key aspect behind the development of the Java programming language, was the need of platform independent language. Many of the characteristics of Java are derived from C++. The Java syntaxes are much similar to C++.

Using Java one can develop all types of applications, likewise;

  • Desktop Application
  • Web Application
  • Distributed Application
  • Mobile Application, etc.

Java Tutorials

Hello friends,

After so many requests from friends & students for the Java Tutorials, I decided to start this series.

Whichever tutorials you like please give your views & feel free to write your suggestions.

Also can request for any topic or  tutorial you require.

Your Friend,

Atul Palandurkar

Check your Operating System & its version

Free Source Code

Free Source Code

Objective :

  • How to check your operating system by programming?
  • How to check your operating system by Java programming?
  • Write Java program to check operating system & its version

Hi folks,

Hey guys, if you want to know which operating system you are using & its version, there are 2 ways to do it as follows;

  • Using Command Prompt
  • Using Programming

1. Using Command Prompt

  • Go to Start Menu
  • Go to Run
  • Type cmd & press Enter/return. Command Prompt will open.
  • Type ver & press Enter/return, you will get the Name of your OS & its Version that you are using
Here I have used, ver command one of the DOS commands which returns Name of Operating System and its version.

2. Using Programming

Here we are using a Java Program to find the operating system you are using & its version :
[sourcecode language=”java”]
package com.si.java.utility.os;

/**
*
* Language : Java
* File : MyOS.java
* Date : Tuesday, October 5, 2010
* @author Atul Palandurkar
* Email : atul.palandurkar@gmail.com
* Website : www.shardainfotech.com
* Blog : http://atulpalandurkar.wordpress.com
*
*/

public class MyOS
{
public static void main(String[] arg)
{
//To find the name of operating system.
String myOS = System.getProperty("os.name");
System.out.println("Operating System : " + myOS);

//To find the version of operating system.
String myOSVersion = System.getProperty("os.version");
System.out.println("Version : " + myOSVersion);
}
}
[/sourcecode]

So, what are you waiting for?
Use it to create various system utilities & have fun.