ShivangiSingh17 / Java-Jet

This repository contains simple Java concepts that I learnt during the course:mag_right: :mag_right:
19 stars 119 forks source link
android hacktoberfest hacktoberfest2021 hacktoberfest2021-accepted java newcomers open-source

GitHub last commit Github coverage Java

alt text

Java

Java is one of the most popular and widely used programming language and platform. It is fast, reliable and secure. Also java is an object-oriented programming language. Java is a statically type language (because the type of data is predefined as part of the programing language and all constants or variables are defined for a given program).

Data Types:

They are basically of two types:

  1. Primitive data type
  2. Object data type

:sound:Primitive consists of boolean, byte, char, short, int, long, float, double. Whereas Object consists of types created by the programmer.

Variables in Java:

A variable is the name given to a memory location. It is the basic unit of storage in a program. When declaring a variable, it needs the data type, variable name and value. For Eg. int cost = 20; Here int is data type ,cost is the variable name and 20 is value.

Java Identifiers:

In Java, an identifier can be a class name, method name, variable name or a label. There are certain rules to define java identifiers:

  1. Only alphanumeric characters are allowed as identifiers i.e. [A-Z], [a-z], [0-9] and '$', '_'.

  2. Identifiers should not start with digit[0-9].

  3. Special characters like @ and & are also not allowed.

  4. Reserved words can't be used.

Here is the first java program-Hello World

Basic Syntax in Java:

About Java programs, there are some very important rules to keep in mind, as follows:

Case Sensitivity

Java is case sensitive, which means identifier Hello and hello would have different meaning in Java.

Class Names

For all class names the first letter should be in Upper Case. If several words are used to form a name of the class, each inner word's first letter should be in Upper Case. This naming pattern is commonly named UpperCamelCase.

Example: class MyFirstJavaClass

Method Names

All method names should start with a Lower Case letter. If several words are used to form the name of the method, then each inner word's first letter should be in Upper Case.

Example: public void myMethodName()

Program File Name

In Java, the name of the program file should exactly match the class name.

:unlock:When saving the file, you should save it using the class name (Remember Java is case sensitive) and append '.java' to the end of the name (if the file name and the class name do not match, your program will not compile).

:unlock:But please make a note that in case you do not have a public class present in the file then file name can be different than class name. It is also not mandatary to have a public class in the file.

Example: Assume 'MyFirstJavaProgram' is the class name. Then the file should be saved as 'MyFirstJavaProgram.java'

public static void main(String args[])

Java program processing starts from the main() method which is a mandatary part of every Java program.

OOP Concept:

alt text

Java is an object oriented programming language. The main aim of OOP is to bind together the data and the functions that operate so that no other part of the code can access this data except that function.

OOPs Concept INCLUDES:

:sound:1)Polymorphism- Way to differentiate between entities with the same name efficiently.

For example:here

:sound:2) Inheritance- Mechanism by which one class can inherit the features of another class.

:sound:3) Object- It is the most basic unit of OOPs. An object consists of:

:sound:4)Classes-Class is a user defined blueprint or prototype from which objects are created. It shows the set of properties that are common to all objects of same type. In general, class declarations includes:

Example of Class and Object together is here.

Object Cloning in Java

Is cloning possible in real life ?? :joy::joy:

alt text

Cloning basically means creating an exact copy of an object. The clone() method of Object class is used to clone an object. The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create. Else clone() method generates CloneNotSupportedException.

Syntax of the clone() method:

protected Object clone() throws CloneNotSupportedException

The clone() method is defined in the Object class.

Why do we need this clone() method??

This method saves the extra processing task for creating the exact copy of an object. If we perform it by using the new keyword, it will take a lot of time, and that's why we use object cloning. Example of the clone() method is here.

Java.lang.StringBuffer.appendCodePoint() Method {RARELY USED}

This method is used to append the string representattion of the codepoint argument. The syntax followed is: public StringBuffer appendCodePoint(int cp).

Parameter:

codePoint − This is a Unicode code point.

Return Value:

The method returns this object after appending the string represented by the codepoint.

Arrays

An array is a type of data structure wherein all elements are of the same data type. Once defined, the size of an array is fixed and cannot increase to accommodate more elements. The first element of an array starts with index zero. Or simply can be defined as a programming construct to define certain categories. It is present in java.util package. There are three-steps involved in defining an array:

Syntax: <elementType> <arrayName>[];

Syntax: arrayname = new dataType[]

Syntax: Eg: intArray[0]=n; // Assigns an integer value (n=any number) to the first element 0 of the array

:mag:To declare an array, define the variable type with square brackets.

While working in array we may need to access the terms that are stored in that an array.We do that by referring to the index number. Arrays can store data in form of primitive data type or in form of an objects of a class. If it stores as primitive data type then data is stored at memory location or else as a heap segment.

There are two types of array we learn about:

:one: One-Dimensionnal Array

:two: Multi-Dimensional Array

So lets discuss in detail;

:one:One-dimensional array declaration has two components: the type and the name.

type declares the element type of the array. The element type determines the data type of each element that comprises the array. Like array of int type, we can also create have an array of primitive data types like char, float, double..etc or user defined data type(objects of a class). Thus, the element type for the array determines what type of data the array will hold.

Array is basically declared as:

:arrow_right: type var-name[];

OR

:arrow_right: type[] var-name;

Now moving on to the next step that is intialising an array.It basically refers to assigning a initial condition/value to the array. Syntax:

variable-name = new type [size];

:warning: type specifies

The type of data being allocated, size specifies the number of elements in the array, and var-name is the name of array variable that is linked to the array. That is, to use new to allocate an array, you must specify the type and number of elements to allocate.

Example:

int intArray[]; //declaring array

intArray = new int[n]; // allocating memory to array

where n=any whole number

:heavy_exclamation_mark:Our above example can we written as this also:

int[] intArray = new int[20]; // combining both statements as one statement.

Array Literals

This type is used when we already know about the array like variables and size of the array.

:mag: There is no need to write the new int[] part.

Accessing Array elements

Once we have an array declared and initialised we can access any element stored in that array by inputing the location of that element. Example we create an array called students and we stored the list of all students with their roll no. then we can access any student name by giving his roll no. and transversing in the array as student[12]
//Let the boy named Deepak is having the roll no.12, so the output will be "Deepak".

Arrays of Objects

Lets at first consider an example .

:question::question:Would you like to guees the output:question::question:. Just like general arrays the difference with the array of objects is that it stores objects in the array whereas the general array stores integers, variables, boolean etc. Also the array elements store the location of reference variables of the object.

Syntax:

Class obj[]= new Class[array_length]

So now lets look toowards the output of the :arrow_double_up:example. The output will be:

Element at 1 : 2 Vaibhav
Element at 2 : 3 Gargi
Element at 3 : 4 Sunil
Element at 4 : 5 Shikhar

Overriding in Java

The benefit of overriding is: ability to define a behavior that's specific to the subclass type, which means a subclass can implement a parent class method based on its requirement.

In object-oriented terms, overriding means to override the functionality of an existing method.