## Java Tutorials

Java is a computer programming language. It is a general-purpose, object-oriented, portable, secure and platform-independent language. It is intended to let application developers "write once, run anywhere" (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.  It was initiated in 1991 by James Gosling, Mike Sheridan, and Patric Naughton. Java was initially called Oak after an Oak tree. Later it named as Java.
There are 8 free resources given below. I have given the link, please follow the like. Happy Learning.

One of my favorite destinations of learning Java is Udemy.com, it is one of the largest and biggest online learning platforms in the world. There are several courses on this portal paid and free. It has many Java courses paid and free.

## Java Program to Convert a Decimal Number to Binary Number

Following Java program convert Decimal number to Binary number. Enter any integer as an input. Now we convert the given decimal input into a binary number with the help of String. We have taken a number in decimal and then done modulus division by 2, the remainder is added/concatenated to a binary number which is a String variable. The program output is also shown below.

Image from wikimedia.org

### Decimal Number to Binary Number

In the program below, a number in Decimal format has been taken from the keyboard and passed to toBinary(int num) method. Where the number is converted by mode division by 2 and added to variable binaryNumber till the number becomes zero. And then return the value.
Source Code:
import java.io.*;
public class DecimalToBinary {

String binaryNumber;        // Calculate the number into Binary as String
int rem;                    // Store the remainder as int
DecimalToBinary()
{
binaryNumber="";
rem=0;
}
// Binary Method
public String toBinary(int num)
{
if (num == 0)
{
return "0";
}

while (num > 0)
{
rem = num % 2;
binaryNumber = rem + binaryNumber;
num = num / 2;
}
return binaryNumber;
}
// Start main method
public static void main(String[] args) throws IOException
{
int decimal;
DecimalToBinary ob = new DecimalToBinary();
System.out.print("Enter a number to convert: ");
String bin = ob.toBinary(decimal); // Decimal to Binary method calling
System.out.println("The binary number representation is " + bin);

} // End of main method
}

Output:
Enter a number to convert: 100
The binary number representation is 1100100
Enter a number to convert: 15
The binary number representation is 1111
Enter a number to convert: 25
The binary number representation is 11001

We can convert decimal to binary in java using Integer.toBinaryString() method also

Source Code:
public class DecimalToBinaryWithMethod
public static void main(String args[])
{
Integer n1 = new Integer(100);
Integer n2 = new Integer(15);
Integer n3 = new Integer(25);
// Convertion using toBinaryString() method
String binary1 = Integer.toBinaryString(n1); // Method toBinaryString(n1)
String binary2 = Integer.toBinaryString(n2);
String binary3 = Integer.toBinaryString(n3);
System.out.println(binary1);
System.out.println(binary2);
System.out.println(binary3);
}

Output:
1100100
1111
11001
In the above program we have used  Integer.toBinaryString() method to convert directly decimal number to binary number and store as String.

# Java Variable

### What is a variable?

Ans. It is nothing but a placeholder of data. It is a memory location. Example int a, b; Here a and b is are variable of data type int.

#### How many types of variables are there in Java?

It has three types. A local variable, an Instance variable, and a Class variable.
• Local Variable: It is defined within the method and constructor. These variables' visibility remains within the method and constructor.
• Instance Variables: Define inside a class. It is available for every object of the class.
• Class variable: when a variable defines with the keyword static inside a class is known as the class variable.
• All objects of the class share static variables because of only a single copy of these available in the memory.

#### java variable naming conventions camelcase

Java variable is written as firstName. This is a camel case, as we write any variable or method, we use all small characters of the first word of the variable and the second first letter will start with a capital letter.

#### State the difference between token and identifier, variable.

Ans. The smallest individual unit of the program is called Token. e.g:  Identifiers, keywords, Literals, etc. whereas an identifier is a name given to different parts of a program, it is also known as variables. e.g. variable, functions, classes, etc.
Variable: A memory location that stores value is known as a variable. It is like a box in memory where we keep or store values. E.g. int a;

#### Explain the Instance Variable. Give an example.

Ans. Variables associated with the instance of a class are called instance variables. E.g.
class ABC
{

int a,b,c;    // Instance variable

void sum()

{

a = 20;

b = 30;

c = a + b;

System.out.println(c);

}
}

Another example

import java.io.*;

public class TaxCalculator
{
// Instance Variables
int pan;
String name;
double taxableincome;
double tax;
void input()throws IOException
{
System.out.println("Enter name and taxable income:");
}
void computeData()
{
if(taxableincome<=60000)
tax = 0;
else if(taxableincome>60000 && taxableincome<=150000)
tax = taxableincome*0.05;
else if(taxableincome>150000 && taxableincome<=500000)
tax = taxableincome*0.1;
else
tax=taxableincome*0.2;
}
void displayData()
{
System.out.println("Name=" + name);
System.out.println("Taxable Income=" + taxableincome);
System.out.println("Tax Paid=" + tax);
}
public static void main(String args[])throws IOException
{
TaxCalculator ob=new TaxCalculator();
ob.input();
ob.computeData();
ob.displayData();
}

}

#### Q. What is the final variable?

When a variable defines with the final keyword then its value can be changed throughout the whole program.
Example
class Circle{
final double pi=3.14;//final variable cannot be change
double a, r;
void area(){
r = 3.5;
a = r*r*pi;
System.out.println("Area=" + a)

public static void main(String args[]){
Circle obj=new  Circle();
obj.area();

}//end of class

#### Q. Can the‘ main’ method be declared final?

Yes, the main method can declare as final.
public class Example
{
public static final void main(String args[])
{
int a=6;
System.out.print((++a)+(a--));

}
}

#### Q. What is a static variable?

A static variable is shared by all instances of a class. It is also known as a class variable and it is declared with the keyword ‘static’.  Instances (objects) of the same class share a single copy of static variables.
class School
int rollno;
String section;
String name;
static String schoolName ="InspireSkills Edu";  // Static variable
// constructor
School(int r, String s, String n){
rollno = r;
section = s;
name = n;

void display ()
{
System.out.println(rollno + " " + section + "   " + name + " " + schoolName);

public static void main(String args[])

School ob1 = new School(1, "A", "Mukesh");
School ob2 = new School(2, "B", "Aryan");

ob1.display();
ob2.display();

/*
* A shop will give a discount of 10% if the cost of the purchased quantity is more than 1000.
*  Ask the user for quantity, Suppose, one unit will cost 100. Judge and print total cost for the user.
*/
import java.util.*;
public class Discount
{
public static void main(String args[])
{
double qty, cost=0.0, tqty,dis;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Quantity:");
qty=sc.nextDouble();
tqty=qty*100;
if(tqty>=1000)
{
dis=tqty*10/100.0; // Percentage
cost=tqty-dis;
System.out.println("Cost="+cost);
}
else
{
cost=qty*100;
System.out.println("Cost="+cost);
}
}
}

More Post
History of java

## History of Java

Java is a computer programming language. It is a general-purpose, object-oriented, portable, secure, and platform-independent language. It was initiated in 1991 by James Gosling, Mike Sheridan, and Patric Naughton. Java was initially called Oak after an Oak tree. Later it named Java. In the year 1995 Sun Microsystems released Java 1.0 and it was based on WORA(Write once Run Any ware) or we can say platform-independent.

Arthur van Hoff was the person who re-wrote Java 1.0 compiler strictly adheres to Java 1.0 specification. In December 1998 Java 2 was released by codename Playground and it has major changes. Java 2 name was changed to J2SE.

In the year 2000 J2SE 1.3 was released with code name Kestrel and it has bundled with Hotspot JVM, JavaSound, Java Naming, JNDI, and Java platform debugger architecture. In the year 2002 J2SE 1.4 released with code name Merlin, it has included regular expression modeled after PERL.

In 2004-2005 J2SE 1.5 released with codename Tiger. In this version, several new features were included like a for-each loop, Generics, autoboxing, and var-args.In 2006 Java SE 6 and dropped zero, with code name Mustang. It has bundled with a database manager and facilitates scripting language with JVM.Java SE 7 with the codename Dolphin in 2011 with new features added such String in switch-case and it supports dynamic language.

In 2014 Java SE 8 was released with lambda expression with code name Spider. The latest released Java SE 9 with Jigsaw as its code name.

The newest version of Java is Java-14, released in March 2020, and Java 11, a currently supported long-term support (LTS) version, released on September 25, 2018. The Oracle Corporation discharged for the Java 8 LTS the last free open update in January 2019 for business use, while it will in any case despite everything bolster Java 8 with open updates for individual utilize something like in any event December 2020. Prophet (and others) energetically suggest uninstalling more seasoned renditions of Java as a result of genuine dangers because of uncertain security issues. Since Java 9, 10, 12, and 13 are no longer supported.

### Major release versions of Java, along with their release dates:

• JDK 1.0 (January 23, 1996)
• JDK 1.1 (February 19, 1997)
• J2SE 1.2 (December 8, 1998)
• J2SE 1.3 (May 8, 2000)
• J2SE 1.4 (February 6, 2002)
• J2SE 5.0 (September 30, 2004)
• Java SE 6 (December 11, 2006)
• Java SE 7 (July 28, 2011)
• Java SE 8 (March 18, 2014)
• Java SE 9 (September 21, 2017)
• Java SE 14 (March 14, 2020)

#### There were five major criteria for the creation of this wonderful language which are as follows:

• Must be an Objected Oriented.
• Must be a secure and robust language.
• Must be portable and architectural neutral which means Java application runs anywhere in networks as it generates bytecode which runs on any OS and networks provided JVM is there.
• Must be high performance

#### Few features of Java.

• General Purpose
• Object-oriented
• Class-based
• Cross-Platform
• Portable
• Architecturally neutral
• Dynamic
• Distributed
• Interpreted Programming Language
• Here emp1 and emp2 are reference data types

More Post
Java Data Types

# Data Types in Java

Data types are used to identify the types of data in a program. It means what kind of data usage is going to put into a memory location.
In Java, there are two categories of data types
•    Primitive data types(Pre-define)
•    Reference data types(Non-primitive)

## Primitive data types:

It loads automatically into the memory as soon as Java program executes. Primitives single a single value. Primitive data types are predefined by the language and named by a keyword.
There are eight primitive data types and it can be divided into four groups.
1. Integer (byte, short, int, double)
2. Floating or real number (float, double)
3. Character (char and it is 16-bit Unicode character)
4. Boolean (Boolean)

 Type Size Range of values that can be stored Default Value byte 1 byte −128 to 127 0 short 2 bytes −32768 to 32767 0 int 4 bytes −2,147,483,648 to 2,147,483,647 0 long 8 bytes 9,223,372,036,854,775,808 to9,223,372,036,854,755,807 0L float 4 bytes 3.4e−038 to 3.4e+038 0.0f double 8 bytes 1.7e−308 to 1.7e+038 0.0d char 2bytes \u0000 to \uFFFF(Maximum Value-65535) Null(‘\0’) Boolean 1 bit true or false false

Java Data Types

Examples:

public static void main(String args[])
{
byte byteVar = 12;
short shortVar = 37;
int intVar =80;
long longVar =898034892149L;
float floatVar = 20.563423424f;
double doubleVar = 20.12323423423468326;
boolean booleanVar = true;
char charVar ='A';

System.out.println("Value Of byte Variable is       " + byteVar);
System.out.println("Value Of short Variable is      " + shortVar);
System.out.println("Value Of int Variable is        " + intVar);
System.out.println("Value Of long Variable is       " + longVar);
System.out.println("Value Of float Variable is      " + floatVar);
System.out.println("Value Of double Variable is     " + doubleVar);
System.out.println("Value Of boolean Variable is    " + booleanVar);
System.out.println("Value Of char Variable is       " + charVar);
}
}

Outputs:
Value Of byte Variable is       12
Value Of short Variable is      37
Value Of int Variable is        80
Value Of long Variable is       898034892149
Value Of float Variable is      20.563423
Value Of double Variable is     20.123234234234683
Value Of boolean Variable is    true
Value Of char Variable is       A

### Reference Data types:

Non-Primitive or Reference data types are formed with the help of primitive data types. It is created by the programmer. It stores the memory address of an object.  The default value of any reference variable is null. Example classes, interface, Arrays, String.
class Employee
{
String name;
int yearOfBirth;
}
class EmpDetail
{
public static void main(String[] args) {
Employee emp1 = new Employee(); // Reference Data type
emp 1.name = "Habib";

Employee emp2 = new Employee();// Reference Data type
emp 2.name = "Sachin";
emp 2.yearOfBirth = 1974;

System.out.println("Name: " + emp 1.name);
System.out.println("Year Of Birth: " + emp 1.yearOfBirth);

System.out.println("Name: " + emp 2.name);
System.out.println("Year Of Birth: " + emp2.yearOfBirth);

}
}

Here emp1 and emp2 are reference data types

More Java Topics
JAVA QUESTIONS AND ANSWERS FOR BEGINNERS