Category Archives: Core Java

Arithmetic, Assignment and Unary Operator in Java

arithmetic operatorIn this chapter you will learn:

  1. What is assignment operator and their symbol?
  2. What is arithmetic operator and their symbol?
  3. What is unary operator and their symbol?
  4. Programming example

What is assignment operator in Java?

Assignment(=) operator is most common operator in Java. It is used for assigning value to variable. For example
int num = 5;

What is arithmetic operator in java?

Arithmetic operator is an operator which provides addition, subtraction, multiplication, division and reminder. It performs mathematical operation on operands.

Operator Description Example
+ It is called as Plus operator which adds integral value. num1 + num2
It is called Minus operator which subtracts integral value. num1 – num2
* It is called Multiply operator which multiply integral value. num1 * num2
/ It is called Divide operator which divide integral value. num1 / num2
% It is called Reminder operator which tells reminder of two integral values. num1 % num2
Programming Example:
class ArithmeticOperators
{
  public static void main(String[] args)
  {
    int num1, num2, result;
    num1=10;
    num2=20;
    
    //Addition
    result=num1+num2;
    System.out.println("Addition : "+result);
    
    //Subtraction
    result=num2-num1;
    System.out.println("Subtraction : "+result);
    
    //Multiplication
    result=num1*num2;
    System.out.println("Multiplication : "+result);
    
    //Division
    result=num2/num1;
    System.out.println("Division : "+result);
    
    //Reminder
    result=num1/4;
    System.out.println("Reminder : "+result);    
  }
}

Output

D:\JavaProgram>javac ArithmeticOperators.java

D:\JavaProgram>java ArithmeticOperators
Addition : 30
Subtraction : 10
Multiplication : 200
Division : 2
Reminder : 2

D:\JavaProgram> __

What is unary operator?

Unary operator is such kind of operator which requires only one operand to perform task. This type of operator is used for increasing or decreasing value, negating the expression or inverting the Boolean value.

Operator Description Example
+ Unary Plus Operator: It indicates positive value. (However numbers are positive by default) 35 or +35
Unary Minus Operator: It indicates negative value -35
++ Increment Operator: It increment value by 1 num1++
Decrement Operator: It decrements value by 1 num1–
! Logical Complement Operator: (also known as Not Operator) – It inverts the Boolean expression. (true to false or false to true) if(!(true))

Programming Example

class UnaryOperator
{
  public static void main(String[] args)
  {
    int number=10;
    
    number++;
    System.out.println(number);
    //number is now 11
    
    number=10;
    number--;
    System.out.println(number);
    //number is now 9
    
    number=10;
    number = -number;
    System.out.println(number);
    //number is now -10
    
    boolean bn=true;
    System.out.println(!bn);
      //bn is now false
  }
}

Output

D:\JavaProgram>Javac UnaryOperator.java

D:\JavaProgram>java UnaryOperator
11
9
-10
false

D:\JavaProgram> __

Summary

In this chapter you have learned about assignment, arithmetic and unary operator in java. The next chapter you will learn about Relational Operator in Java.

Learn Java Operators with Programming Example

In this chapter you will learn:Relational-operator-in-java

  1. What is an operator?
  2. How many types of operators are available in Java?
  3. Programming examples of Operator in java

What is an Operator?

Operators are the special symbol which performs specific tasks on variables as addition, subtraction, compare, check condition, assign value etc.

In the previous chapter you have learned how to initialize variable in Java. In this chapter you will know to performing operation on defined variable.

How many types of operators available in Java?

  1. Arithmetic, Assignment AND Unary Operator
  2. Relational Operator
  3. Logical OR Conditional Operator
  4. ( ? : ) Conditional/Ternary Operator
  5. Bitwise Operator

Summary

In the next chapter we will learn about all these operators one by one. We also tried to provide complete programming example to you so that you can understand the entire topic easily. Next chapter you will learn about Assignment, Arithmetic and Unary Operator in Java.


Variables and DataTypes in Java – Programming Example

variables and datatypesIn this chapter you will learn:

What is variable and DataType in java?
How many types of DataType is available in java?
Programming example of Variables and DataType in java

Variables and datatype is the basic building block of any programming language. Without it, you can’t do a single job in programming language. So, in this chapter you are going to learn very important part of Java Programming.

What is variable?

Variable is nothing just a location in memory which keeps store data on it. All the programming language needs the help of variable to store data while processing or executing program. For example, when you do a simple job in program in which you need to add two numbers and show output, you need a memory location in which you could store the value before showing it on the console.

 For Example:

Memory Location

In the above pictorial example, when you initialize an integer variable to keep a numeric value in it, it allocates a memory location and store numeric value passed by program.

What is Data Type in Java?

Data Types is basically a term which defines which type of value is going to be stored in variable. For example if you define variable with integer data type it only holds numeric value in it.

How many data types are available in Java?

There are two types of datatype available in Java – Primitive Data Type and Non Primitive Data Type

Primitive DataTypes

Primitive data types are reserved keyword and already defined by Java. Primitive data types directly hold the value. There are 8 types of primitive data types in java.

 

DataType Size Range Default Value
byte 8 bit signed -128 to 127 0
short 16 bit signed -32,768 to 32,767 0
int 32 bit signed -2,147,483,648 to 2,147,483,647 0
long 64 bit signed -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 0L
float single-precision 32-bit 0.0f
double double-precision 64-bit 0.0d
boolean 1 bit true or false false
char 16-bit Unicode character ‘\u0000’ (or 0) to ‘\uffff’ (or 65,535 inclusive) ‘\u0000’

 

Programming Example of Java DataTypes

class VariablesAndDatatypes
{
  public static void main(String[] args)
  {
    byte bt=20;
    short st=30;
    int it=40;
    long lg=50L;
    float ft=60;
    double db;
    boolean bn=true;
    
    db=bt+st+it+lg+ft;  
    if(bn==true)
    {
    System.out.println(db);
    }    
  }
}

Output

D:\JavaProgram>javac VariablesAndDatatypes.java

D:\JavaProgram>java VariablesAndDatatypes
200.0

D:\JavaProgram> __

 

Non-Primitive Data Type

In java, non-primitive data types are simply called objects. It is sometimes called referenced variables or object references. Non primitive data type doesn’t store the value directly instead of it stores the location of variables. Strings, array etc are the non-primitive data types.

 

Summary

In this chapter you have learned what is variable and datatypes in java. You have also seen a complete example of java data types. In the next chapter you will learn about operators in Java.

Java Programming Example for Practice

49aa65231961cd3b64ec0482ee5ca636_400x400In this chapter you will learn:

  1. Some more java programming example

The main purpose of adding this chapter is providing your some more java programing example. When you completed this chapter you will be familiar with how to write, save, compile and execute java program. What you all need is to do only write this code is in your favorite code editor, save them with same name of class, compile them and finally run them for getting output.

This chapter contains 4 programming example. You don’t need to dive into complexity; just try to learn how to write and execute java program.

Qu 1: Write a program for adding two numbers. Display the output.
Answer
class AddTwoNumbers
{
  public static void main(String[] args)
  {
    int a, b, result;
    a=20;
    b=30;
    result=a+b;
    System.out.println("Add = "+result);
  }
}
Output

D:\JavaProgram>javac AddTwoNumbers.java

D:\JavaProgram>java AddTwoNumbers
Add = 50

D:\JavaProgram> __

 

Qu 2: Write a program for displaying your good name.
Answer
class DisplayName
{
  public static void main(String[] args)
  {
    System.out.println("My Name is Alex");
  }
}
Output

D:\JavaProgram>javac DisplayName.java

D:\JavaProgram>java DisplayName
My Name is Alex

D:\JavaProgram> __

 

Qu 3: Write a program for displaying “Hello friends, How are you?”.
Answer
class HelloFriends
{
  public static void main(String[] args)
  {
    System.out.println("Hello Friends, How are you?");
  }
}
Output

D:\JavaProgram>javac HelloFriends.java

D:\JavaProgram>java HelloFriends
Hello Friends, How are you?

D:\JavaProgram> __

 

Qu 4: Write a program for multiplying 2 numbers.
Answer
class MultiplyNumbers
{
  public static void main(String[] args)
  {
    int result=20 * 30;
    System.out.println(result);
  }
}
Output

D:\JavaProgram>javac MultiplyNumbers.java

D:\JavaProgram>java MultiplyNumbers
600

D:\JavaProgram>

 

Summary

In this chapter you have learned some more programming examples in java. This is only for making you comfortable with java. The next chapter is Variables and Datatypes in which you will learn creating variable with appropriate data types.

How to Compile and Execute your 1st Java Program

numbers-1stIn this chapter you will learn:

JavaEditor – For writing colorful java code
Write your first java program
Compiling and executing Java Program

We assume that your PC is ready for java development. In this chapter we will write simple java program and will learn how to compile program using JVM and execute bytecode for getting output. But before writing java program, you must have a java editor to write program.

Where to write Java Program?

However, you can write your entire java program in notepad but I personally recommend that not to use notepad to write java program. The basic reason behind this is notepad is simple text editor and you will never get color coding as well as hierarchy coding view. It is very pathetic to develop java class in notepad.

There are lots of java editor are available freely on the net. You can download any of them. I am using DrJava as a java editor. It is free, light weight, easy to use and gives better view of java coding. You can download it from here.

http://www.drjava.org

DrJava

First Java Program

Step 1: Make a blank folder named JavaProgram in D drive or any other drive. This folder will contain your entire java program.

 

Step 2: Open DrJava Editor or notepad editor and write/paste following code.

 

class HelloWorld
{
  public static void main(String[] args)
  {
    System.out.println("Welcome to the world of Java");    
  }
}

Step 3: Save it with the name of HelloWorld.java

 

Note: Make sure to save your file with the same name of java class name. In the program we have created a class HelloWorld so we have saved this program with the name HelloWorld.java

Compile and Debug using command prompt

As you are using DrJava editor you can directly compile and execute your program but it is necessary to know how to compile and run your program using command prompt.

Step 1: Open command prompt

 

Step 2: Go to your folder location. Type D: and press enter. Now type cd JavaProgram and press Enter.

Java Path

Step 3: now type javac HelloWorld.java and press Enter button. If everything goes fine you won’t get any error message. It means your program is successfully converted into bytecode.

Compile Java

Step 4: Type java HelloWorld to execute your program.

Run Java Program

Note: When you compile your java program, it creates one more file with the same name of class but extension is .class.

class java

Compile and Debug using DrJava Editor

It is so much easy to compile and execute java program using this editor. There is dedicated button for compiling and executing your program. Let’s see how it works!

Dr Java Editor

Summary

In this chapter you have learned how to write and execute java program. You have also learned about java editor. In the next chapter you will learn some more java programming example. By doing all the question you will be familiar with java language.

Java Installation and Path Setting with screenshots

Java InstallationIn this chapter you will learn:

  1. How to Download and Install Java in Windows 10/8/7, Vista or XP?
  2. How to set Java Path in Different OS?
  3. Ready to write and execute Java Program

This is very fundamental chapter of learning java in which you will learn downloading Java, Installation and setting path. If you know how to signup in facebook you are capable in doing all these tasks yourself. It is as easy as posting your status on facebook or twitter. Believe me, I am not joking. I will guide you at every steps with screenshots and at the end you feel proud on yourself.

 

Download Java

If your PC doesn’t have Java, download it from java official site. Go to Java Official Site

Installation:

After downloading Java, it’s time to install it on your PC. It is not a difficult task and as easy as installing other windows software.

Steps 1: Double click on java executable file which you have downloaded.

Java Executable File

Steps 2: The installation process is started. Windows security will ask you whether allow or not to run this software. Click yes if asked.

Permission

Steps 3: Click on Install button to accept license terms and start Installation.

Permission

Steps 4: Now just do Next > Next > Finish.

Permission

Steps 5: Congratulations!!! You have installed Java Successfully on your PC.

 

Now, it’s time to check whether Java is installed on your PC or not.

Go to C Drive > Program Files. Here in the list you will find Java folder.

Java Location

If you get this folder it means Java has been installed on your PC. But it doesn’t mean you are ready for writing and executing java program. One more step is ahead. It is setting Path in System Environment Variable. Until you set path you are not able to compile java code.

Set Java Path in System Environment Variable

In order to setting java path you need to open system variable windows first. To open system variable windows you follow these steps.

Windows 10/8/7, Vista and XP

Steps 1: Right click on MyComputer, Computer or This PC and select properties.

Steps1

Steps 2: In the left side, there is link Advanced System Settings. Click on it.

steps 2

Steps 3: Go to Advanced tab and click on Environment Variables.

steps 3

Steps 4: In the System Variable Column click on New.

steps 4

Steps 5: In the Variable Name write JAVA_HOME and in the Variable Value field Write Java Installation path. It is in your program file location as C:\Program Files\Java\jdk1.8.0_65

steps 5

Steps 6: Click on OK to save.

 

Steps 7: Now find Path in the variable name and select it.

Steps 7

Steps 8: Click on Edit

steps 7

Steps 9: Add %JAVA_HOME%\bin at the end. You must put a semicolon (;) at the end of previous value before adding new value. Do not delete or alter previous written value. Softly put a semicolon at the end if there is no semicolon and then put your JAVA_HOME value.

steps 9

Steps 10: Click OK to save.

 

Testing Java

Now it’s time to test whether Java is working on your machine or not. In order to test java, do the following steps.

1. Open command prompt. To open command prompt press windows + R key and write cmd and press enter. Write javac in the command prompt. If you get list of java option then cheers! Java is ready on your machine and now you can start java journey.

Java cmd

Summary

In this chapter you have learned how to Download, Install and set path for Java. Setting environment variable is little bit tricky but if you are learning Java then this level of tech is expected from you. In the next chapter you will write and execute your first JAVA program.


Getting Started – Core Java

core-java
In this chapter you will learn

  1. How Java Works?
  2. Advantages of Java
  3. What are Java Compiler and Interpreter?

Welcome to the first chapter of core java. It is the very first chapter and from here you will start your journey. This journey is surely going to adventures and challenging where you will find a new way see the world using java programming. As, we promised to provide complete java programming for each topics and deliver less theory; it is always our mind. But before starting the journey of JAVA, do you know over 3 million devices are using java these days. Your calculators, DVD Player, Printing machine, digital watch, remote control, television etc are working because of java. So, the simple question which you are thinking why java, why not other languages? If you are thinking this question then surely you are smarter one. Answer is very interesting and you must know the structure of Java code.

How Java Works?

If you compare Java to other high level programming language, the bigger difference which you would find is platform independency. For example if you compile C++ code it will only work on windows platform. It will never work on linux machine. To work on linux machine you will have to make another version of your program.

On the other hand, Java is platform independent. If you compile your code with Java compiler it will work on windows, linux and all other platform which is Java enabled. Java compiles its code into bytecode using JVM (Java Virtual Machine). Bytecode is not an executable file but it is precompiled and ready to be executed using java interpreter. A java enabled machine get this bytecode and execute them with the help of interpreter.

There is 2 steps of compiling and executing Java program.

1. JVM (Java Virtual Machine) – Compile Java Program into bytecode using javac command.

2. Java Interpreter – Execute java bytecode using java command.

Java bytecode model

How to setup my PC for Java Programming?

Don’t worry if your machine is not set or configured for java programming. We assume that you have just bought your PC and no software is installed. We will start from scratch and gives you fully details of each topic with graphical as well as programmatically help. In the next chapter you will learn How to configure your PC for java programming. This chapter includes

1. Downloading and installing java

2. Setting path (windows xp, windows vista/7/8/10)

3. Checking whether java is working or not

4. Writing a demo java program and executing them.

So, what are you waiting for? Be ready for configuring your machine if you haven’t configure yet for JAVA.

Summary

In this chapter you have learned the importance of Java Programming in short. It is practical based learning so, the next entire lesson you will understand java with real time programming. Next chapter is downloading and Installing Java.