× Home

First Java program - Input, output, debugging and datatypes

First Java Program

Structure of java file

"Source code that we write will be saved using extension .java"

  • Everything written in .java file must be in classes or we can say that every file having .java externsion is a class.
  • A class with same name as file name ust be present in .java file.
First alphabet of class name can be in upper case. It is the naming convention of class name. However, it is not compulsory to do so.
  • Class which is having same name as file must be public class.
  • A main function/method must be present in this public class, main is a function from where the program starts.

Converting .java to .class

  • Using javac compiler we can convert .java file to class commant to convert .java to .class
    • Let the name of .java file is Main. So the command to convert .java to .class is javac Main.java

Running the program

  • By using java and name of file we can run the program
    • command → java Main

Hello world program

                        

                            public class Main{
                                public static void main(String[] args){
                                    System.out.println("Hello World");
                                }
                            }
                        
                    
  1. public (first line) → public is an access modifier which allows to access the class from anywhere
  2. class → It is a name group of properties and functions
  3. Main → It is just the name of class as same as the name of file.
  4. public (second line) → It is used to allow the program to use main fruntion from anywhere
  5. static → It is a keyword which helps the main method to run without using objects.
  6. void → It is a keyword used when we don not want to return anything from a method/function
  7. main → It is the name of method.
  8. String [] args → It is a command line argument of string type array.
  9. System → It is a final class defined in java.lang package.
  10. out → It is a variable of PrintStream type which is public and static member field of the System class
  11. println → It is a method of PrintStream class, it prints the arguments passed to it and adds a new line. print can also be used here but it prints only arguments passed to it, it do not adds a new line.

What is package?

  • It is just a folder in which java files lies.
  • It is used to provide some rules and stuff to our programs.

Primitive data types

  • Primitive data types are thoses data types which is not breakable
    • Ex → String is not a primitive data type so we can break this data type into char.
    • i.e.. String "Kunal" can be divided into → 'K' 'u' 'n' 'a' 'l'
    • but primitives data type are not breakable
    • we cannot break a char, int etc.

List of primitive data types in java are :-

Data types

Description

Example

int

stores numeric digit

int i = 26;

char

stores characters

char c = 'A';

float

stores floating point numbers

float f = 98.67f;

double

stores larger decimal numbers

double d = 45676.58975;

long

stores numeric digit which is not able to stored in int

long l = 12234354354365l;

boolean

It only stores true and false

boolean b = false;

In float and long we have used f an dl. it denotes that the number in the variable is float or long type. If we don not use this java consider float value as double and long value as int.

  • Literals → It is synthtic representation of boolean, character, string, and numeric data.
    • Ex :- int a = 10
      here 10 is called literal
  • Identifiers → name of variable, methods, class, packages, etc. are known as identifiers.
    • Ex:- int a = 10;
      Here a is identifier.

Comments in java

Comments are something which is written in source code but ignored by the compiler.

  • Two type of comment
    1. Single line comment :- used to comment down a single line (// is used for it.)
    2. Multi line coment :- used to comment down multiple lines (/* */ is used for it.)

Inputs in java

We have Scanner class available in java.util package to take input

  • To use this class we have to
    1. Import java.util package in our file
    2. Create object of the scanner class
    3. Use that object to take input from the keyborad.

                        

                            //syntax
                            import java.util.Scanner;
                            public class Main{
                                public static void main(String [] args){
                                    Scanner input = new Scanner(System.in);
                                }
                            }
                        
                    
  1. Scanner → It is a class required to take input, it is present in java.util package.
  2. input → It is an object that we are creating to take input.
  3. new → It is a keyword used to create an object in java.
  4. System.in → System is a class and in is a variable that denotes we are taking input from standard input stream (i.e. keyboard)

int input → nextInt() is a function used to take input of int.

                            
    
                                Scanner input = new Scanner(System.in);
                                int rollno = input.nextInt();
                            
                        

float input → nextFloat() is a function used to take input of float.

                            
    
                                Scanner input = new Scanner(System.in);
                                float marks = input.nextFLoat();
                            
                        

string input →
Two ways to take string input

                            
    
                                // 1. using next() methods - It will take one word input till a space occurs
                                Scanner input = new Scanner(System.in);
                                String s1 = input.next();
                            
                        
                            
    
                                // 2. using nextLine() methods - It will take all string input including spaces.
                                Scanner input = new Scanner(System.in);
                                String s2 = input.nextLine();
                            
                        

Program for sum of two numbers

                            
    
                                import java.util.Scanner;
                                public class Sum {
                                    public static void main(String[] args) {
                                        Scanner input = new Scanner(System.in);
                                        System.out.print("Enter first no. ");
                                        int num1 = input.nextInt();
                                        System.out.print("Enter second no. ");
                                        int num2 = input.nextInt();
                                        int sum = num1+num2;
                                        System.out.println("The sum is ="+ sum);
                                    }
                                }
                            
                        

Type Conversion

When one type of data is addigned to another type of variable an automatic type conversion will take place under some condition

  • Conditions :-
    1. Two types should be compatible
    2. Destination type should be greater then the source type.

Type Casting

When we convert one type of data to another type is known as type casting.
Ex :- int num = (int) (67.3455f)

Automatic type promotion in expressions

While evaluating expressions the intermediate value may exceed the operands and hence the expression value will be promoted.

There are some condition for type promotion :-

  • Java automatically promotes each byte, short or char operand to when evaluating an expression.
  • If one operand is a long, float or double the whole expression is promoted to long, float or double respectively.
                                
        
                                    byte a = 40;
                                    byte b = 50;
                                    byte c = 100;
                                    int d = (a*c)/c;
                                    System.out.println(d);
                                
                            

Here when a*b occurred it became 2000 which is out of the range of byte (as byte max can be 256). So, here byte is automatically promoted to int type.

Example for thorough review concept.

                                
        
                                    public class TypePromotion {
                                        public static void main(String[] args) {
                                        byte b =42;
                                        byte c = 'a';
                                        short s = 1024;
                                        int i = 50000;
                                        float f = 5.67f;
                                        double d = 0.1234;
                                        double result = (f*b)+(i/c)+(d*s);
                                        System.out.println((f*b)+" "+(i/c)+" "+(d*s));
                                        System.out.println(result);
                                        }
                                        }
                                
                            

Explicit type casting

If we want to store large value into small data type
Ex : byte b = 50;
b = (byte) (b*2); // type casting int to byte.