First Program

Suryansh GrSuryansh Gr
3 min read

Every file that ends with .java is a class.

The public class in the file can be only the name of the file or public class has the same name as of the file

public ---> everyone can access

public static void main(String[] args){}

this is the main function in a class, that will be executed first or from where the program starts.

In terminal :

\>> javac Main.java (compilation, file name)

\>> java Main (running)

static ----> variables and object that doesn't depend on object

String[] args ---> string array for storing command line arguments.

Path variables?

enviroment variables ?

package is a folder (com.suryansh)

To print:

System.out.println("");

System ---> is a class in lang package

out ---> variable of printstream(CL)

println ---> method.

Input in JAVA:

import java.util.Scanner (package)

creating a new object : Scanner input = new Scanner(System.in);

input.next() ---> string (just a word before space)

input.nextLine() ----> string (whole line)

input.charAt(n) ----> to get char at n (given index).

Primitive Datatype : you can't break further.

Integer : int n = 6;

Character : char letter = 'a';

Float : float marks = 98.67f; (f --> to specify float type, by default double)

Double: double largedecimalnumber = 45678907.45678

Long : long integer = 3456788627L; (L --> to specify long, by default integer)

Boolean : boolean check = true;

Type Casting :

Automatic type casting Rules:

  • Type should be compatible eg. float and int.

  • The destination type size should be greater than the source type.

Or manual

int num = (int)(67.56f);

Automatic Type promotion in expressions :

Example 1 :

int a = 257;

byte b = (byte)(a);

System.out.println(b);

>>> 1

Since a byte can store max 256, if it is greater than that then it will use the formula 257 % 256 = 1.

Example 2 :

byte a = 40;

byte b = 50;

byte c = 100;

int d = a*b/c;

System.out.println(d);

>>> 20

Since byte \ byte = byte, but 40 \ 50 = 2000 and bytes can only store a max value of 256 and 2000 exceeds the range, how this program has executed?

This program has been executed with the help of Automatic type conversion,

here java is promoting each byte into Integer while evaluating this expression.

Consider this example:

byte b = 50;

b = b*2;

>>> Error : Integer can't be assigned into byte.

Since b = b*2; expression is an integer.

Example 3 :

int number = 'A';

System.out.println(number);

>>> 65

character is converted into its corresponding ASCII value integer.

(A --> 65)

  • All the byte, short and char values are promoted to integer.

  • If any operand is long, double or float, then the whole operation will be promoted to corresponding (long, double or float). or small will be promoted to big in an expression.

Example 4 :

byte b = 42;

char c = 'a';

short s = 1024;

int i = 50000;

float f = 5.67f;

double d = 0.1234;

double result = (f + b) + (i/c) - (d*s);

Here the expression (f + b) will be of float and byte will be promoted to float,(i/c) char will be promoted to integer and (d*s) short will be promoted to double.

Now the whole expression will be in terms of datatype :

float + int - double : Now in this expression float and int will be promoted to double and the whole expression will become double.

double

Java follows unicode

System.out.println("नमस्कार");

>>> नमस्कार

To read more about unicode : Unicode - Wikipedia

0
Subscribe to my newsletter

Read articles from Suryansh Gr directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Suryansh Gr
Suryansh Gr

I am a tech enthusiast and a learner