Java 1 Intro, Variable Datetype Oper
Java 1 Intro, Variable Datetype Oper
Java was developed by Sun Microsystems in the year 1995. James Gosling is
known as the father of Java. Before Java, its name was Oak. Since Oak was already
a registered company, so James Gosling and his team changed the name from Oak
to Java.
Standalone Application
Web Application.
Mobile Application (Currently, Android and Java ME are used for creating
mobile applications.
C++ vs java:
Java doesn't support default arguments like C++.
Java does not support header files like C++. Java uses the
import keyword to include different classes and methods.
Design Goal C++ was designed for Java was designed and
systems and created as an
applications interpreter for printing
programming. It was systems but later
an extension of the C extended as a support
programming network computing. It
language. was designed to be
easy to use and
accessible to a broader
audience.
2. Features of Java
Simple - Java is very easy to learn, and its syntax is simple, clean and easy to
understand.
Java syntax is based on C++ (so easier for programmers to learn it after C+
+).
Java has removed many complicated and rarely-used features, for example,
explicit pointers, operator overloading, etc.
Platform independent -
If any language code written on 1 operating system and run it on that same OS, After that
same code copy and execute on another Operating System, If that copied code not execute
on another OS then that language code is called as PLATFORM DEPENDENT code. Java
compiler javac converts the program code into byte code. This byte code
is platform-independent and can run on any JVM operating system. JVM
interprets the byte code to machine code, and the program is executed.
Secured – because there no pointers, we have used sand box alternatively to store
the data.
Robust - strong
Interpreted(clearly explained)
High Performance
Download
Install the JDK if you don't have installed it, download the JDK and install it.
For Example:
set path=C:\Program Files\Java\jdk1.6.0_23\bin
Go to MyComputer properties -> advanced tab -> environment variables -> new tab
of user variable -> write path in variable name -> write path of bin folder in variable
value -> ok -> ok -> ok
To javac
compile: Simple.java
To
java Simple
execute:
class Simple
{
public static void main(String args[])
{
System.out.println("Hello Java");
}
}
class keyword is used to declare a class in Java.
void is the return type of the method. It means it doesn't return any value.
String[] args or String args[] is used for command line argument. We will discuss it
in coming section.
Loads code
Verifies code
Executes code
4.Java Variables
A variable is a container which holds the value while the Java program is executed.
There are three types of variables in java: local, instance and static.
1.Local Variable
A variable declared inside the body of the method is called local variable. You can
use this variable only within that method. A local variable cannot be defined with
"static" keyword.
2) Instance Variable
A variable declared inside the class but outside the body of the method, is called an
instance variable. It is not declared as static.
A variable which is created inside the class but outside the method is
known as an instance variable. Instance variable doesn't get memory at
compile time. It gets memory at runtime when an object or instance is
created. That is why it is known as an instance variable.
3) Static variable
A variable that is declared as static is called a static variable. It cannot be local. You
can create a single copy of the static variable and share it among all the instances of
the class. Memory allocation for static variables happens only once when the class
is loaded in the memory.
public class A
{
int data=50;//instance variable
static int m=100;//static variable
void method()
{
int n=90;//local variable
}
public static void main(String args[])
{
}
}//end of class
5. Data type:
Data types specify the different sizes and values that can be stored in the variable.
There are two types of data types in Java:
Primitive types are predefined (already defined) in Java. Non-primitive types are
created by the programmer and is not defined by Java (except for String).
Non-primitive types can be used to call methods to perform certain operations, while
primitive types cannot.
A primitive type has always a value, while non-primitive types can be null.
A primitive type starts with a lowercase letter, while non-primitive types starts with an
uppercase letter.
Examples of non-primitive types are Strings, Arrays, Classes, Interface, etc. You will
learn more about these in a later chapter.
Widening Casting
Widening Casting (automatically) - converting a smaller type to a larger type
sizebyte -> short -> char -> int -> long -> float -> double
Widening casting is done automatically when passing a smaller size type to a larger
size type:
int myInt = 9;
System.out.println(myInt);
System.out.println(myDouble);
Narrowing Casting
Narrowing Casting (manually) - converting a larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte
Narrowing casting must be done manually by placing the type in parentheses in front
of the value:
System.out.println(myDouble);
System.out.println(myInt);
6.Operators in Java
Operator Category Precedence
Type
additive +-
equality == !=
bitwise ^
exclusive
OR
bitwise |
inclusive OR
logical OR ||
Ternary ternary ?:
Shift
Java Left Shift Operator Example
public class OperatorExample{
public static void main(String args[]){
System.out.println(10<<2);//10*2^2=10*4=40
System.out.println(10<<3);//10*2^3=10*8=80
System.out.println(20<<2);//20*2^2=20*4=80
System.out.println(15<<4);//15*2^4=15*16=240
}}
The logical && operator doesn't check the second condition if the first condition is
false. It checks the second condition only if the first one is true.
The bitwise & operator always checks both conditions whether first condition is true
or false.
Output:
false
false
Output
false
10
false
11
Output:
true
true
true
10
true
11
Output:
2
Assignment Operator
Java assignment operator is one of the most common operators. It is used to assign
the value on its right to the operand on its left.
public class OperatorExample{
public static void main(String[] args){
int a=10;
a+=3;//10+3
System.out.println(a);
a-=4;//13-4
System.out.println(a);
a*=2;//9*2
System.out.println(a);
a/=2;//18/2
System.out.println(a);
}}
Output:
Compile time error
break: Java break keyword is used to break the loop or switch statement. It breaks
the current flow of the program at specified conditions.
byte: Java byte keyword is used to declare a variable that can hold 8-bit data
values.
case: Java case keyword is used with the switch statements to mark blocks of text.
catch: Java catch keyword is used to catch the exceptions generated by try
statements. It must be used after the try block only.
char: Java char keyword is used to declare a variable that can hold unsigned 16-bit
Unicode characters
continue: Java continue keyword is used to continue the loop. It continues the
current flow of the program and skips the remaining code at the specified condition.
default: Java default keyword is used to specify the default block of code in a switch
statement.
do: Java do keyword is used in the control statement to declare a loop. It can iterate
a part of the program several times.
double: Java double keyword is used to declare a variable that can hold 64-bit
floating-point number.
enum: Java enum keyword is used to define a fixed set of constants. Enum
constructors are always private or default.
extends: Java extends keyword is used to indicate that a class is derived from
another class or interface.
final: Java final keyword is used to indicate that a variable holds a constant value. It
is used with a variable. It is used to restrict the user from updating the value of the
variable.
finally: Java finally keyword indicates a block of code in a try-catch structure. This
block is always executed whether an exception is handled or not.
float: Java float keyword is used to declare a variable that can hold a 32-bit floating-
point number.
for: Java for keyword is used to start a for loop. It is used to execute a set of
instructions/functions repeatedly when some condition becomes true. If the number
of iteration is fixed, it is recommended to use for loop.
if: Java if keyword tests the condition. It executes the if block if the condition is true.
import: Java import keyword makes classes and interfaces available and accessible
to the current source code.
int: Java int keyword is used to declare a variable that can hold a 32-bit signed
integer.
interface: Java interface keyword is used to declare an interface. It can have only
abstract methods.
long: Java long keyword is used to declare a variable that can hold a 64-bit integer.
null: Java null keyword is used to indicate that a reference does not refer to
anything. It removes the garbage value.
package: Java package keyword is used to declare a Java package that includes
the classes.
public: Java public keyword is an access modifier. It is used to indicate that an item
is accessible anywhere. It has the widest scope among all other modifiers.
return: Java return keyword is used to return from a method when its execution is
complete.
short: Java short keyword is used to declare a variable that can hold a 16-bit
integer.
static: Java static keyword is used to indicate that a variable or method is a class
method. The static keyword in Java is mainly used for memory management.
super: Java super keyword is a reference variable that is used to refer to parent
class objects. It can be used to invoke the immediate parent class method.
switch: The Java switch keyword contains a switch statement that executes code
based on test value. The switch statement tests the equality of a variable against
multiple values.
synchronized: Java synchronized keyword is used to specify the critical sections or
methods in multithreaded code.
this: Java this keyword can be used to refer the current object in a method or
constructor.
throw: The Java throw keyword is used to explicitly throw an exception. The throw
keyword is mainly used to throw custom exceptions. It is followed by an instance.
transient: Java transient keyword is used in serialization. If you define any data
member as transient, it will not be serialized.
try: Java try keyword is used to start a block of code that will be tested for
exceptions. The try block must be followed by either catch or finally block.
void: Java void keyword is used to specify that a method does not have a return
value.
volatile: Java volatile keyword is used to indicate that a variable may change
asynchronously.
while: Java while keyword is used to start a while loop. This loop iterates a part of
the program several times. If the number of iteration is not fixed, it is recommended
to use the while loop.
Input Types
In the example above, we used the nextLine() method, which is used to
read Strings. To read other types, look at the table below:
Method Description
nextBoolean( Reads a boolean value from the user
)
class Main {
System.out.println("Enter username");