Java Programming

Thursday, May 30, 2019

Learn java Hello World

Here is a program which prints Hello World!.
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}
The first line is a syntax of defining a class called HelloWorld.
public class HelloWorld {
The next line is the main method to be able to run program.
public static void main(String[] args) {
  • public anyone can access it.
  • static anyone can use or call this method without creating an instance of the class.
  • void method doesn't return any value.
  • main name of the method.
  • String[] args is method arguments and it is an array of string. The argument stores the value of parameters if the program is run with any parameter.
The next line is to print Hello World! in the program.
System.out.println("Hello World!");
  • System is the System class contains several useful class fields and methods.
  • out is a static method of class System and a "standard" output stream corresponds to display output in the program.
  • println is to print a String and then terminate a line.
An example with Eclipse:


Wednesday, May 29, 2019

Learn Variables and Data Types in Java

There are two data types in Java:
  • Primitive Data Type
  • Reference/Object Data Type

List of all primitive data types in Java:

  • byte (number, 1 byte)
  • short (number, 2 bytes)
  • int (number, 4 bytes)
  • long (number, 8 bytes)
  • float (float number, 4 bytes)
  • double (float number, 8 bytes)
  • char (a character, 2 bytes)
  • boolean (true or false, 1 byte)

Syntax

How to declare and assign a primitive data type in Java:
type vairable_name;
vairable_name = value;
Or
type vairable_name1, vairable_name2;
variable_name1 = value
variable_name2 = value; 
Or
type vairable_name = value;

byte

  • Byte data type is an 8-bit signed two’s complement integer.
  • Minimum value is -128
  • Maximum value is 127
  • Default value is 0
  • Byte data type is used to save memory in large arrays, mainly in place of integers because byte is four times smaller than an int.
How to declare and assign byte number in Java:
byte yourByte = 100;
An example with Eclipse:

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)
  • Default value is 0
  • Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an integer.

How to declare and assign int number in Java:

short yourShort = 1000;

An example with Eclipse:
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)
  • Default value is 0L
  • This type is used when a wider range than int is needed

How to declare and assign long number in Java:
long yourLong = 1000L;

An example with Eclipse:

 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)
  • The default value is 0
  • Integer is generally used as the default data type for integral values unless there is a concern about memory.
How to declare and assign int number in Java:
int yourInt = 2;
An example with Eclipse:

float

  • Float data type is a single-precision 32-bit IEEE 754 floating point
  • Default value is 0.0f
  • Float is mainly used to save memory in large arrays of floating point numbers
How to declare and assign float number in Java:
float yourFloat = (float) 4.4;

Or
float yourFloat = 4.4f;

An example with Eclipse:

double

  • double data type is a double-precision 64-bit IEEE 754 floating point
  • Default value is 0.0d
  • This data type is generally used as the default data type for decimal values
How to declare and assign double number in Java:
double yourDouble = 3.3;

An example with Eclipse:

char

  • char data type is a single 16-bit Unicode character
  • Minimum value is '\u0000' (or 0)
  • Maximum value is '\uffff' (or 65,535 inclusive)
  • Char data type is used to store any character
How to declare and assign char or a character in Java:
char yourChar = 'c';
An example with Eclipse:

boolean

  • boolean data type represents one bit of information
  • There are only two possible values: true and false
  • This data type is used for simple flags that track true/false conditions
  • Default value is false
How to declare and assign boolean in Java:
boolean yourBoolean = false;
yourBoolean = true;
An example with Eclipse:

List of all object data types in Java:


Monday, April 1, 2019

Conditional

Java uses boolean variable to check the condition. The value can be only true or false compared or checked with the condition.

if statement in Java

For example:
int a = 1;
int b = 1;

if (a==b) {
    System.out.println("It's true!");
}
if (a==1) { 
    System.out.println("It's true!");
}
if (b==1) { 
    System.out.println("It's true!");

}

if else statement in Java

For example:
int a = 2;

if (a==1) {
    System.out.println("It's true!");
} else { 
    System.out.println("It's false!");
}

else if statement in Java

For example:
int a = 2;

if (a==1) {
    System.out.println("a is 1!");
} else if (a==2) { 
    System.out.println("a is 2!");
}

boolean operators in Java

int a = 3;
int b = 4;
boolean result;
result = a < b; // true
result = a > b; // false
result = a <= 3; // a smaller or equal to 3 - true
result = b >= 5; // b bigger or equal to 5 - false
result = a == b; // a equal to b - false
result = a != b; // a is not equal to b - true
result = a > b || a < b; // Logical or - true
result = 2 < a && a < 5; // Logical and - true
result = !result; // Logical not - false

== and equals

  • == is used to compare with primitive data type
  • equals is used to compare with object data type
For example:
String a = new String("Hello");
String b = new String("Hello");
if (a.equals(b)) {
     System.out.println("It is true!");
}

Sorry, this page is under construction.

under construction!