Contact : +88 01840-723661

|

Email : info@jakafind.com

Java Programming Learning Tutorial P001

Java Programming Learning Tutorial P001

Introduction to Java

@ Java is a high-level programming language originally developed by Sun
@ Microsystems and released in 1995.
@ Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.

Features of Java

Object Oriented: In Java, everything is an Object. Java can be easily extended since it is based on the Object model.

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 virtual Machine (JVM) on whichever platform it is being run.

Simple: Java is designed to be easy to learn. If you understand the basic concept of OOP Java would be easy to master.

Secure: With Java’s secure feature it enables to develop virus-free, tamper-free systems. Authentication techniques are based on
public-key encryption.

Architectural-neutral: Java compiler generates an architecture-neutral object file format which makes the compiled code to be
executable on many processors, with the presence of Java runtime system.

Portable: Being architectural-neutral and having no implementation dependent aspects of the specification makes Java portable.

Compiler in Java is written in ANSI C with a clean portability boundary which is a POSIX subset.

Robust: Java makes an effort to eliminate error prone situations by emphasizing mainly on compile
time error checking and runtime checking.

Multithreaded: With Java’s multithreaded feature it is possible to write programs that can do many
tasks simultaneously. This design feature allows developers to construct smoothly running interactive
applications.

Interpreted: Java byte code is translated on the fly to native machine instructions and is not stored
anywhere. The development process is more rapid and analytical since the linking is an incremental and
light weight process.

High Performance: With the use of Just-In-Time compilers, Java enables high performance.

Distributed: Java is designed for the distributed environment of the internet.

Dynamic: Java is considered to be more dynamic than C or C++ since it is designed to adapt to an
evolving environment. Java programs can carry extensive amount of run-time information that can be
used to verify and resolve accesses to objects on run-time.

Tools you will need

For performing the examples discussed in this tutorial, you will need a Pentium 200-MHz
computer with a minimum of 64 MB of RAM (128 MB of RAM recommended)
You also will need the following software:
• Operating system.
JDK (Java Development Kit)
• Java editor

Popular Java Editors

To write your Java programs, you will need a text editor. There are even more
sophisticated IDEs available in the market. But for now, you can consider one of
the following:

Notepad: On Windows machine you can use any simple text editor like
Notepad, TextPad.

Netbeans: is a Java IDE that is open-source and free which can be
downloaded from http://www.netbeans.org/index.html

Eclipse: is also a Java IDE developed by the eclipse open-source community
and can be downloaded from http://www.eclipse.org/

Simple Java Program


public class MyFirstJavaProgram
{
public static void main(String []args)
     {
System.out.println("Hello World");
     }
}

Java Identifiers

• Names used for classes, variables and methods are called identifiers.
• All identifiers must follow the following rules:
• An identifier is a sequence of characters that consists of letter (A to Z or a to z), digits(0 to 9),
currency character ($) or an underscore (_).
• All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an
underscore (_).It can not start with a digit (0-9).
• A keyword cannot be used as an identifier.
• Most importantly identifiers are case sensitive.
area, Area, AREA are all different identifiers
Examples of legal identifiers: age, $salary, _value, __1_value
Examples of illegal identifiers: 123abc, -salary

 

Basic Operators in Java

1. Arithmetic Operators
2. Relational Operators
3. Bitwise Operators
4. Boolean Logical Operators
5. Conditional Operator ( ? : )

1. Arithmetic Operators

• Addition +
• Subtraction –
• Multiplication *
• Division /
• Remainder %
• Increment ++
• Addition Assignment +=
• Subtraction Assignment -=
• Multiplication Assignment *=
• Division Assignment /=
• Modulus Assignment %=
• Decrement —

Arithmetic Assignment Operators

Operator Example Equivalent To Increment/decrement
+=
-=
*=
/=
%=
x += y
x -= y
x *= y
x /= y
x %= y
x = x + y
x = x – y
x = x * y
x = x / y
x = x % y
• Preincrement:  i = ++n;
• Predecrement: i = –n;
• Postincrement: i = n++;
• Postdecrement: i = n–;

 

2. Relational Operators

• > greater than
• >= greater than or equal to
• < less than
• <= less than or equal to
• = = equal to
• != not equal to
• The outcome of these operations is a boolean value.
• = = , != can be applied to any type in java.
• Only numeric types are compared using ordering operator.

3. Bitwise Operators
~ Bitwise unary NOT
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
>> Shift Right
>>> Shift Right zero fill
<< Shift left
& = Bitwise AND Assignment
|= Bitwise OR Assignment
^= Bitwise XOR Assignment
>>= Shift Right Assignment
>>>= Shift Right zero fill Assignment
<<= Shift Left Assignment

4. Boolean Logical Operators

& Logical AND
| Logical OR
^ Logical XOR
|| Short-circuit OR
&& Short-circuit AND
! Logical unary NOT
&= AND Assignment
|= OR Assignment
^ = XOR Assignment
= = Equal to
!= Not equal to
?: Ternary if-then-else

5. Conditional Operator ( ? : )
(expression) ? value if true : value if false
(a > b) ? a : b;
it is an expression which returns one of two values, a or b. The condition, (a > b), is tested. If it is
true the first value, a, is returned. If it is false, the second value, b, is returned.

if (a > b) {
max = a;
}
else {
max = b;
}
max = (a > b) ? a : b;
Conditional Operator is equivalent to if ..else

Taking input from User – Scanner Class

‘Scanner’ class is used to take input from user
An object of Scanner class is created to read input
Java uses System.out to refer to the standard output device and System.in to standard input device
Import ‘java.util.Scanner’ package to use scanner class Commonly used methods for Scanner class

• nextByte(): reads an Byte value from the user
• nextShort() : reads an Short value from the user
• nextInt() : reads an int value from the user
• nextLong() : reads an Long Integer value from the user
• nextFloat() : reads an Float value from the user
• nextDouble() : reads an Double value from the user
• next() : reads a word from the user
• nextLine() : reads a Line of Text from the user

Exercise:
Using Scanner class
• Take two integer numbers as input and calculate their sum.
• Calculate the area of a circle. Take radius as input.
• Take some information about yourself (i.e. name, age, cgpa, department, section etc) as input and display them.

1. import java.util.*;
2. public class ScannerExample {
3. public static void main(String args[]){
4. Scanner in = new Scanner(System.in);
5. System.out.print(“Enter your name: “);
6. String name = in.nextLine();
7. System.out.println(“Name is: ” + name);

Exercise:
• Check whether a number is even or odd.
• Check whether a number is positive or negative.
• Check whether a year is LEAP YEAR or Not.
• Find out the maximum value of three numbers.
• Make a simple calculator.

Thanks for your time!

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *