0% found this document useful (0 votes)
7 views

Java

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Java

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

What is 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.

Java is a popular programming language, created in 1995.

It is owned by Oracle, and more than 3 billion devices run Java.

It is used for:

 Mobile applications (specially Android apps)


 Desktop applications
 Web applications
 Web servers and application servers
 Games
 Database connection

Why Use Java?


 Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
 It is one of the most popular programming language in the world
 It is easy to learn and simple to use
 It is open-source and free
 It is secure, fast and powerful
 It has a huge community support (tens of millions of developers)
 Java is an object oriented language which gives a clear structure to programs
and allows code to be reused, lowering development costs
 As Java is close to C++ and C#, it makes it easy for programmers to switch to
Java or vice versa.

JAVA VIRTUAL MACHINE

A Java virtual machine (JVM) is a virtual machine that enables a computer to run
Java programs as well as programs written in other languages that are also
compiled to Java bytecode.

JVM Architecture
Let's understand the internal architecture of JVM. It contains classloader,
memory area, execution engine etc.
History of Java
James Gosling initiated Java language project in June 1991 for use in one of his many
set-top box projects. The language, initially called “Oak” after an oak tree that stood
outside Gosling's office, also went by the name ‘G‘Oak’Green’ and ended up later
being renamed as Java, from a list of random words.
Sun released the first public implementation as Java 1.0 in 1995. It promised Write
Once, Run Anywhere (WORA), providing no-cost run-times on popular platforms.
On 13 November, 2006, Sun released much of Java as free and open source software
under the terms of the General Public License (GPL).
Let us now briefly look into what do class, object, methods, and instance variables
mean.
 Object − Objects have states and behaviors. Example: A dog has states - color,
name, breed as well as behavior such as wagging their tail, barking, eating. An
object is an instance of a class.
 Class − A class can be defined as a template/blueprint that describes the
behavior/state that the object of its type supports.
 Methods − A method is basically a behavior. A class can contain many
methods. It is in methods where the logics are written, data is manipulated and
all the actions are executed.

Sample Program
public class MyFirstJavaProgram
{
/* This is my first java program.
* This will print 'Hello World' as the output
*/
public static void main(String[] args) {
System.out.println("Hello World"); // prints Hello World
}
}

Java IO : Input-output in Java

Java brings various Streams with its I/O package that helps the user to perform all
the input-output operations.
3 standard or default streams that Java has to provide which are also most
common in use:
1. System.in: This is the standard input stream that is used to read characters from
the keyboard or any other standard input device.

2. 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.

Here is a list of the various print functions that we use to output statements:
 print(): This method in Java is used to display a text on the console. This text
is passed as the parameter to this method in the form of String. This method
prints the text on the console and the cursor remains at the end of the text at
the console.

Syntax:

System.out.print(parameter);
// Java code to illustrate print()
import java.io.*;

class Demo_print {
public static void main(String[] args)
{

// using print()
// all are printed in the
// same line
System.out.print("GfG! “);
System.out.print("GfG! ");
System.out.print("GfG! ");
}
}

println(): This method in Java 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.

Java - Basic Datatypes


There are two data types available in Java −
 Primitive Data Types
 Reference/Object Data Types

Primitive Data Types


There are eight primitive datatypes supported by Java. Primitive datatypes are
predefined by the language and named by a keyword.

Byte
 Byte data type is an 8-bit signed two's complement integer
 Minimum value is -128 (-2^7)
 Maximum value is 127 (inclusive)(2^7 -1)
 Default value is 0

short
 Short data type is a 16-bit signed two's complement integer
 Minimum value is -32,768 (-2^15)
 Maximum value is 32,767 (inclusive) (2^15 -1)
int
 Int data type is a 32-bit signed two's complement integer.
 Minimum value is - 2,147,483,648 (-2^31)
 Maximum value is 2,147,483,647(inclusive) (2^31 -1)

long
 Long data type is a 64-bit signed two's complement integer
 Minimum value is -9,223,372,036,854,775,808(-2^63)
 Maximum value is 9,223,372,036,854,775,807 (inclusive)(2^63 -1)
 This type is used when a wider range than int is needed
 Default value is 0L
float
 Float data type is a single-precision 32-bit
 Float is mainly used to save memory in large arrays of floating point numbers
 Default value is 0.0f

Non-primitive data types - such as String, Arrays and Classes

public class Main

public static void main(String[] args) {

int myNum = 5; // integer (whole number)

float myFloatNum = 5.99f; // floating point number

char myLetter = 'D'; // character

boolean myBool = true; // boolean

String myText = "Hello"; // String

System.out.println(myNum);

System.out.println(myFloatNum);

System.out.println(myLetter);

System.out.println(myBool);
System.out.println(myText);

Type casting
Convert a value from one data type to another data type is known as type casting.

Types of Type Casting


There are two types of type casting:

1.Widening Type Casting


2. Narrowing Type Casting

Widening Type Casting


Converting a lower data type into a higher one is called widening type casting. It is also known as
implicit conversion or casting down. It is done automatically. It is safe because there is no chance to
lose data. It takes place when:
1.Both data types must be compatible with each other.
2.The target type must be larger than the source type.

byte -> short -> char -> int -> long -> float -> double

Exmple
public class WideningTypeCastingExample
{
public static void main(String[] args)
{
int x = 7;
//automatically converts the integer type into long type
long y = x;
//automatically converts the long type into float type
float z = y;
System.out.println("Before conversion, int value "+x);
System.out.println("After conversion, long value "+y);
System.out.println("After conversion, float value "+z);
}
}

Narrowing Type Casting


Converting a higher data type into a lower one is called narrowing type casting. It is also
known as explicit conversion or casting up. It is done manually by the programmer. If we
do not perform casting then the compiler reports a compile-time error.

double -> float -> long -> int -> char -> short -> byte
Example
public class NarrowingTypeCastingExample
{
public static void main(String args[])
{
double d = 166.66;
//converting double data type into long data type
long l = (long)d;
//converting long data type into int data type
int i = (int)l;
System.out.println("Before conversion: "+d);
//fractional part lost
System.out.println("After conversion into long type: "+l);
//fractional part lost
System.out.println("After conversion into int type: "+i);
}
}

Java Variables

Declaring (Creating) Variables

To create a variable, you must specify the type and assign it a value:

Syntax
type variableName = value;
Example
public class Main {
public static void main(String[] args) {
String name = "John";
System.out.println(name);
}
}
Example
public class Main {
public static void main(String[] args) {
int myNum = 15;
System.out.println(myNum);
}
}
The Arithmetic Operators
Arithmetic operators are used in mathematical expressions in the same way that they
are used in algebra. The following table lists the arithmetic operators −
Assume integer variable A holds 10 and variable B holds 20, then −
Show Examples

Operator Description Example

Adds values on either side of the operator. A+B


+ (Addition) will give
30

Subtracts right-hand operand from left-hand A-B


- (Subtraction) operand. will give
-10

Multiplies values on either side of the A*B


* (Multiplication) operator. will give
200

Divides left-hand operand by right-hand B/A


/ (Division) operand. will give
2

Divides left-hand operand by right-hand B%A


% (Modulus) operand and returns remainder. will give
0

Increases the value of operand by 1. B++


++ (Increment)
gives 21

Decreases the value of operand by 1. B-- gives


-- (Decrement)
19

Example program for arithmetic operator

public class ArithmeticOperatorDemo


{
// Demonstrate the basic arithmetic operators.
public static void main(String args[])
{
// arithmetic using integers
System.out.println("Integer Arithmetic");
int i = 1 + 1;
int n = i * 3;
int m = n / 4;
int p = m - i;
int q = -p;
System.out.println("i = " + i);
System.out.println("n = " + n);
System.out.println("m = " + m);
System.out.println("p = " + p);
System.out.println("q = " + q);
// arithmetic using doubles
System.out.println("\nFloating Point Arithmetic");
double a = 1 + 1;
double b = a * 3;
double c = b / 4;
double d = c - a;
double e = -d;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
System.out.println("e = " + e);
}
}

You might also like