[Go to site: main page, start]

0% found this document useful (0 votes)
13 views12 pages

Java Tutorials for Beginners

This document provides a comprehensive introduction to Java programming for beginners, covering its history, requirements, and basic structure. It explains data types, user input handling using the Scanner class, typecasting, and functions in Java with example codes. The tutorial aims to equip learners with foundational knowledge and practical skills in Java programming.

Uploaded by

Javed Tufail
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views12 pages

Java Tutorials for Beginners

This document provides a comprehensive introduction to Java programming for beginners, covering its history, requirements, and basic structure. It explains data types, user input handling using the Scanner class, typecasting, and functions in Java with example codes. The tutorial aims to equip learners with foundational knowledge and practical skills in Java programming.

Uploaded by

Javed Tufail
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

NATIONLA UNIVERSITY OF MODERN LANGUAGES, RAWALPINDI

Java Tutorials (For Absolute Beginners)

Azaz Kiani
Java Tutorials
History
Requirements:
1. Java Development Toolkit (JDK)
2. IDE
Most Common IDEs are:
1. Eclipse
2. NetBeans
3. InteliJ IDEA

Java History and Background:


Developed by Sun Microsystems in 1991
James Gosling was one of the founders of Java
Initially Called Oak
Based on C and C++ but eliminate some features of C and C++
Purely Object Oriented Programming Language.

Basic Structure of Code


Code:

public class MyClass{


public static void main(String[] args){
[Link]("Hello World");
}
}
1. Data Types in Java

There are two basic types of variables in java


 Primitive Data Types
 Non- Primitive data types

Primitive Data Types


Primitive type is predefined by the language and is named by a reserved keyword.
Java offers eight primitive types: byte, short, int, long, float, double, char, and boolean.

Non Primitive Data Types


Non-primitive data types, also known as reference types, represent complex data and more
sophisticated structures than primitive types. They include classes, arrays, interfaces, and more.
Unlike primitive types that store actual values, non-primitive types store references to the objects in
memory.
Data Types Examples:

Integral Data Types (Program 1)


(Same code as in above fig but in plain text)

// Java Tutorial 2
// Data types in Java
public class DataTypes {
public static void main(String[] args) {
// byte - 8-bit integer
byte b1 = 12;
[Link](b1);

// short - 16-bit integer


short s1 = 21;
[Link](s1);
// int - 32-bit integer
int i1 = 112;
[Link](i1);

// long - 64-bit integer


long l1 = 234453L;
[Link](l1);
}
}

Data Types Example 2 (Program 2)

Code
(Same code as in above fig (example 2) but in plain text)

// Data Types Program 2


public class DataTpes {

public static void main(String[] args){


float f1 = 23.45F; // float data type
[Link](f1);

double d1 = 23.45D; //Double data type


[Link](d1);

boolean bol = true;// Boolen float data type


[Link](bol);

char c1 = 'A'; // Char float data type


[Link](c1);

String str = "Our Java Class"; //String


[Link](str);
}
}

2. Taking input from user:

In JAVA w e use scanner Class to take input from user.


First we have to import the Scanner Class.
Next we create the object of Scanner Class. As in OOP we have to create the object of class to use the
class.
Basic Syntax of creating object is:
Class_name object_name = new class_name.
Where new is key word

Example Program
Next() function is used to accept next values. You can use next function as per your
requirements. Here are some next functions.

Code:
(Same code as in above fig but in plain text)

//Taking Input From user


import [Link]; // Import Java Class Scanner

public class input {

public static void main(String[] args){


//Create a new object Named scn of scanner Class
Scanner scn = new Scanner([Link]);

[Link]("Enter a Value");

int num1 = [Link]();


// nextInt is Scanner class function to take intput
[Link]("You entered " +num1);
}
}

Sum two numbers by taking input from user


Example 1: Program 1
//Sum two numbers by taking input from user
import [Link];
public class calculator{

public static void main(String [] args){


Scanner scn = new Scanner ([Link]);
[Link]("Enter number 1");
int num1 = [Link]();
[Link]("Enter number 2");
int num2 = [Link]();
int Sum = num1 + num2;
[Link]("the Sum is:" +Sum);

}
}
Example 2: Program 2
Same program as above but with different data types
import [Link];

public class MyClass {

public static void calculate() {

Scanner scn = new Scanner([Link]);

[Link]("Enter Number 1: ");

long num1 = [Link]();

[Link]("Enter Number 2: ");

long num2 = [Link]();

long num3 = num1 + num2;

[Link]("The sum is: " + num3);

public static void main(String[] args) {

calculate();

Example 3: Taking String as Input


import [Link];

public class MyClass{

public static void main (String [] args){

Scanner scn = new Scanner([Link]);

[Link]("Enter your Name:");

String name = [Link]();

[Link](name);

}
3. Typecasting in Java
Typecasting: Converting one data type to another data type.
Java can perform conversion automatically

Example: int value can be assigned to long


Not all type conversions implicitly allowed in java.

For example: Can’t assign a long value to int


One general rule is : Narrow data types are converted into broad data type without loss of information

Example:

int myInt = 9;

// Automatic casting: int to double

double myDouble = myInt;

Some examples of implicit type conversion in java (Code)

In above program a is of type short and we want to store it in int, so this is permit able.
Next c is of type short and we want to store it in type long it is also permit able
Similarly i is of type float and we want to store it in double, also permit able.
These are example of type casting done by compiler as we are store a short (i.e. small ) data type into
large.
Narrow data types are converted into broad data type without loss of information

[Link] in Java
Example 1: Sum two numbers by taking input from user and using
separate function

Note: Sum function is created to perform all calculations and then this function is called in our main
function
Example 2: We can also pass the parameters to function (as you learned in c++)

You might also like