#java programming

Java Programming

Chapter 1

What is Java?

  • Java is an object-oriented programming language.
  • Popular programming language.
  • It is strongly typed language

.Class File

A class file in Java has a .class extension. It contains bytecode, which is instruction for Java Virtual Machine, which translates that bytecode into platform-specific machine-level instruction based upon whether the Java program runs on Windows or Linux.

First Java Program.

public class Main {
    public static void main(String[] args) {
        System.out.println("Welcom to Java programming");
    }
}

Chapter 2

Scanner Class

https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html

  • Scanner class is found in the java.util.package
  • Scanner class is use to get input from the user in Java
  • It can parse primitive types and strings using regex.
Scanner input = new Scanner(System.in); //Create an instance of the scanner 
System.out.print("Enter a double value: ");
Scanner input = new Scanner(System.in);
double d = input.nextDouble();
System.out.println("Your value is " + d);

Import Package

import java.util.* ; // Implicit import
import java.util.Scanner; // Explicit Import
No performance difference

Variable

  • A container that holds the value
  • Strongly typed

Primitive Data Types

  • Primitive data types are stored by Value. It is the fastest possible memory.
  • Numbers, characters, and Booleans values .
  • Primitive type names are all lowercase.
  • Primitive values are always signed they support positive and negative values.
  • btye, int,char,short, long, double, float, boolean
  • String is not primitive type.It is an object. It is a complex object, an instance of a class.
  • https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

How to declare a variable?

int myVariable =20;
int => Data type
myVariable => identifier
= Assigment
20 = value

Starting Java 10, local variables can use type inference

var myVariable=20;

DataTypes example

        int firstValue=100;
        int secondValue=firstValue;
        firstValue=50;
        secondValue=70;
        System.out.println(firstValue);
        System.out.println(secondValue);

Explicit Types in Literal Values

var  myIntValue=5;
var  myFloatValue=5f;
var myDoubleValue=5d;
var myLongValue=5L or 5l

Stored by Value

Constants

  • A variable whose value cannot be changed once it is assigned.
  • Constant the variable name on constants
  • final datatype CONSTANTNAME = VALUE;  
final int VALUE=123;

Constant example

Type Casting

Converting the value from one data type to another data type

Implicit casting
double data= 3; (type widening)
Explicit casting
int i = (int)3.0; (type narrowing)
int i = (int)3.9; (Fraction part is truncated)

Chapter 3

Selections

How do we compare two values in java?

byte value1=1;
byte values2=2;
boolean result=(value1>value2);
System.out.println(result);

Result: false

Java if condition

Java if condition checks for the decision-making statement based on its true or false condition.

if(condition) 
{
   // Statements to execute if the given condition is true
  
}
---------------------------------
if (condition)
{
   // Run this block if
 condition is true
}
else
{
  // Run this block if
 condition is false
}
-----------------------------------------------------
if (condition)
    statement;
else if (condition)
    statement;
.
.
else
    statement;

Switch

Execute the code based on the value of the switch expression value.

switch (expression)
{
  case value1:
    statement1;
    break;
  case value2:
    statement2;
    break;

  default:
    statementDefault;
}