Software engineer

Java Introduction

Java Introduction

Java

Java is a programming language and computing platform first released by Sun Microsystems in 1995, and it is owned by Oracle, and more than 3 billion devices run Java.

Basic Java Syntax

public static void main (String args[]){

  • Attribute

    • public static:Called from any class
    • private static:Only permit to called in same class
    • static:same as public static attribute
    • common class:
      • String Class: Used for manipulating strings, including string concatenation, formatting, and operations.
      • ArrayList Class: Provides a dynamic array with variable size, used to store and manipulate multiple objects.
      • Scanner Class: Used for reading input from the user.
      • Math Class: Provides various mathematical operations, such as computing mathematical constants and performing math calculations.
      • File Class: Used for file and directory handling, including creating, reading, and writing files.
      • Date Class: Used for handling dates and times.
      • Random Class: Used for generating random numbers.
      • HashMap Class: Provides a key-value mapping, used for storing and querying data.
      • Thread Class: Used for multithreaded programming, implementing synchronization.
  • return type

    • void: Indicates that the method does not return any value.
    • int, double, float, etc.: Represents numeric types and returns corresponding numeric values.
    • boolean: Represents a boolean value (true or false).
    • String: Represents a sequence of characters and returns a text value.
    • Object: Represents a reference to an object of any class.
    • Array: Represents an array of elements.
    • Custom Class: You can define your own classes and use them as return types to encapsulate more complex data.
  • Method

public Project3(){}
	    public void testing(){
        for (int j =0;j<10;j++){
            for(int i =0;i<100;i++){
                if (i==5){
                    return; 
                }
                System.out.println(j+""+i);
                }
            }
        }
        
  • Method Overlaoding
Java can define the type of the parameter.
public class Prac{
    public Prac(){

    }
    public void addition(int a, int b){
        System.out.println(a+b);
    }
    public void addition(String a, String b){
        System.out.println(a+b);
    }

    public static void main(String[] args) {
        Prac m=new Prac();
        m.addition("14","13");
        m.addition(7,8);
    }
}
//output:15(int)
//output:1413(String)
class test{
        private int x, y;
        public test(int x, int y){//constructor
                this.x = x;//this():Called same class constructor.
                this.y = y;//super():Called parent class constructor from child class.
        }
        public void summary(){//method 
                System.out.println(x + y);
        }
}
public class example{
        public static void main(String[] args){
                test x = new test(3, 5);
                x.summary();
        }
}
//output:8
comments powered by Disqus