Udemy logo

java courseThe Java programming language incorporates several powerful features of other equally powerful programming languages. Thanks to Java’s numerous libraries, developers can build a wide range of applications.  Java is an object-oriented programming language, which makes it easy to learn for the beginner. The scope and demand of Java is emerging exponentially in the IT market since it is used in web development, mobile development, Hadoop and analytics. In addition, the Android platform and its libraries are also built in Java.

If you’ve never dealt with Java before, you don’t need to fret as the concepts are very simple. Assuming that you have no knowledge about Java, we will start with the very basics. Since Java is platform-independent, you can run it on any OS whether it’s Linux, Windows or Mac. Before you start off, you need to install the Java Virtual Machine. You can download it through the official Sun Microsystems website. This is a very brief introduction, but you will get a clear picture once you follow the code examples mentioned below.

Learn Java from scratch by taking a course at Udemy.com

Objects and Classes

Objects act as the basic foundation to Java. They are very similar to the objects we see in the real world. Just like the objects in real life vary in their behavior, complexity and state, software objects behave similarly too. The only difference between a class and an object is that an object is based on the class. While the classes are capable of storing data and performing specific functions, they specify an action to be carried out and objects perform these actions.

How to Declare a Class

Now that you have a basic idea of what a class means, you can declare it.

A simple example of declaring a class:

class MyFirstClass
{
// variables declarations
// functions declarations
}

The above example shows a class declaration. The area in between the braces “{“is known as the class body and it contains the code that is necessary for the object’s initialization. You should also note that a class can have subclasses within them. In addition, you can create a lot more information about the class at the beginning of a class declaration.

Become familiar with Java by taking a course at Udemy.com

For example:

class MyFirst extends SuperClass
{
// fields, constructor
// function declarations
}

Here, the subclass is “MyFirst” and “SuperClass” is the parent class. The subclass can extend the functionality of the parent class. This is how you inherit a class. The “child” class inherits the parent’s methods and properties, so you don’t need to rewrite code for every class.

Let’s start with a Hello World example:

class Hello // Class declaration
{
public static void main  (String []args) // Main method (Compulsory Function)
{
System.out.println("My First Program. Hello World'); // Message gets printed on the Screen
}
}

The above program will tell you a lot about Java programming. The class named “Hello” is declared with a “main” method defined inside the class. Every Java program has this main function, since it is the starting point for program execution. “System.out.println” prints the message “My First Program. Hello World” to the screen. Incidentally, every Java program is written inside of a class.

Features of Java:

Syntax for the if statement

if(Condition)
{
// body
}
else
{
//body
}

Nested if-else Statements

Syntax:

if(Condition 1)
{
// body
}
else if(Condition 2)
{
// body
}
else if(Condition 3)
{
// body
}
else
{
//body
}

The Switch Statement:

Syntax:

switch(choice)
{
case 1: //Statements
break;
case 2: //Statements
break;
Default: System.out.println("Wrong Choice");
}

Ternary Operator : This is similar to an if statement.

Syntax:

 (Condition1: Condition2)? True; False;

Conditional Statement Example:

class Largest // Class declaration
{
public static void main(String args[])  // main method declaration
{
int a,b; // Initialization of the Variables of integer data type
char choice;
System.out.println("Enter two integers "); // Method prints message on screen
Scanner in = new Scanner(System.in);
a = in.nextInt();
b = in.nextInt(); //User Input
if ( a > b ) // Condition check for Largest number
{
System.out.println("1st number is Larger.");
}
else
{
System.out.println("2nd number is Larger.");
}
}

Output:

Enter two integers 6 10

2nd number is Larger.

Example Of a Switch Statement:

import java.util.*; //Importing the packages
class SwitchExample
{
public static void main(String[] args)  //main method declaration
{
int x, y; // Declaring the variable
Scanner scant=new Scanner(System.in);
System.out.println("Enter 2 numbers:");
x = scant.nextInt();  //User input
y= scant.nextInt();
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("Enter Your Choice");
int a= scant.nextInt();
switch (a) //Switch statements started
{
case 1:
System.out.println("Case one Sum is =" + (x+y));
break; //Prevents execution of  other cases
case 2:
System.out.println("Case two Difference is =" + (x-y));
break;
default: // This is executed when no case is matched. It is optional statement
System.out.println("Invalid Choice!");
}
}
}

Output:

Enter 2 numbers: 12 5

1. Add

2. Subtract

Enter Your Choice 1

Case one Sum is =17

The choice input is 1 therefore the first case gets executed and the sum of the two numbers is calculated.

Looping: Like other languages, Java also supports three looping constructs. Looping occurs when a set of statements has to be executed in a definite or infinite interval of time. The three looping constructs are for, while and do-while.

Syntax: For loop

for(initialization; condition; increment/decrement)
{
//statements to execute
}

While loop:  It is generally used when the number of repetitions are not known in advance.

Syntax:

while(Condition)
{
// Set of statements
}

Do-While Loop: This is used when the loop is to be executed at least one time before the condition  is being checked.

Syntax:

do
{
// set of statements
}while(Condition);

Example of Loops:

import java.util.Scanner; //Importing the Scanner class used for taking user input
class Factorial
{
public static void main(String args[]) //main method declaration
{
int n, c, fact = 1;
System.out.println("Enter a number"); // Message to  user
Scanner in = new Scanner(System.in);
n = in.nextInt();
if ( n < 0 ) //if statement condtion
System.out.println("Number should be non-negative.");
else
{
for ( c = 1 ; c <= n ; c++ ) // For loop Implementation To calculate Factorial of Number
{
fact = fact*c;
}
System.out.println("Factorial of "+n+" using For loop = "+fact);
}
int x=1;
fact=1;
while(x<=n)  // While loop Implementation To calculate Factorial of Number
{
fact = fact*x;
x++;
}
System.out.println("Factorial of "+n+" using While loop = "+fact);
}
}

Output

Enter a number 5

Factorial of 5 using For loop = 120

Factorial of 5 using While loop = 120

The factorial is calculated using for and while loop of the number entered by the user. Both the loops gives the same answer.

Strings and String Manipulation: Strings manipulation is one of the best and the most important part of Java course. String class has many built in functions, which helps in string manipulations. Some String class functions are:

toUpperCase()

toLowerCase()

str.length()

str.concat(“String to add”)

String Manipulation Example :

import java.util.*;
class StringDemo
{
public static void main(String args[])
{
String str1, str2 = "";
Scanner scant = new Scanner(System.in);
System.out.println("Give me a String");
str1 = scant.nextLine();//String input
int length = str1.length(); //Returns the length of the string passed
System.out.println("String Length "+length);
str2=str1.toUpperCase(); // converts the string to uppercase
System.out.println("String in Upper Case" +str2);
str2=str1.toLowerCase(); //converts the string to lowercase
System.out.println("String in LowerCase"+str2);
str2=str1.concat( "I am new String"); //Sums up the string with the passed value
System.out.println("String added "+str2);
}
}

Output:

Give me a String Hello String

String Length 12

String in Uppercase  HELLO STRING

String in Lowercase  hello string

String added Hello String I am new String

The result of all the values returned by the string functions are printed on the screen.

Learn more about Java by taking a course at Udemy.com

Functions: Java provides features that you can declare and use these classes and functions. This helps us to make the code easier to handle and debug during testing. Grouping code in classes and functions provides reuse of your code. This means that you can import your classes from other other programs and reuse them in current programs. Java classes can be accessed by making their objects and the class objects in turn are used to access the functions declared inside the class.

Syntax:

class hello
{
public int MyFirstFunction()
{
// Function body
return
}
}

“MyFirstFunction” is the name of the function that is declared with int data type. We can declare function of any primitive data type. For instance

public int MyFirstFunction()

public double MyFirstFunction()

public char MyFirstFunction()

public float MyFirstFunction()

The data type of function declared tells us that what type of value will a function return. If we use void instead of data type that means the function does not return any value.

Syntax:

 public void Myfunction()

“public” is an access specifier which decides the accessibility of the functions in other classes. Function declared public is accessible by all the classes. Other specifier are “private”  and “protected”. “private” function is only accessible in same class while protected function is accessible b.y all the classes of same package(group of classes). This specifier helps in data hiding the important feature of object oriented programming language

private int MyFirstFunction()
{
// Function body
return
}
 
protectedintMyFirstFunction()
{
// Function body
return
}

Conclusion: This article explains the basics of Java for beginners. It is a vast topic and hence you will have to practice a lot more to get it right. However, Java becomes easy if you have an understanding of the concepts of Object Oriented Programming.

Page Last Updated: June 2014

Top courses in Java

Java for Beginners
Navin Reddy
4.5 (1,813)
Java SE 11 Developer 1Z0-819 OCP Course - Part 1
Tim Buchalka, Tim Buchalka's Learn Programming Academy
4.5 (4,087)
Bestseller
Learn Selenium with Java, Cucumber & Frameworks
Pavan Kumar
4.6 (8,355)
Bestseller
Modern Java - Learn Java 8 Features By coding it
Pragmatic Code School
4.5 (11,318)
Java Interview Help
Bharath Thippireddy
4.5 (1,631)

More Java Courses

Java students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo