top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Java For Beginners Part - 2

Introduction

In our last blog (Java For Beginners Part -1 - Link https://www.numpyninja.com/post/java-for-beginners-part-1 )we discussed few of basic java topics. In this blog we will continue remaining topics Arrays, Methods in java, Method overloading, classes and Objects , Java Constructors , Java Modifiers, Enums, Java Interface, Java Iterator, ArrayList, LinkedList, HashMap, HashSet with examples and few with Output screenshots.

Arrays

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

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

Array indexes start with 0. [0] is the first element.

[1] is the second element, etc.

In the below example we can see how to change an element in array, array length, loops in array

Example :

1. int [] num = {1,2,3,4};

2.



Multi-dimensional Arrays

A multidimensional array is an array containing one or more arrays.

In two dimensional array we have to add each array with its own set of curly braces.

Example :

Java Methods

Method in Java or Java Method is a collection of statements that perform some specific task and return the result to the caller. A method is a block of code which only runs when it is called. Methods are also known as functions.

A Method also be called multiple times.

Example : 1. Method and method calling

2. Method and calling method multiple times


3. Methods with multiple Parameters

Static Vs Non-Static

Static method can be accessed without creating an object of the class.

Non- Static method can be accessed only by each objects which is created for that class.

In the above example we created static method so we did not create any objects to call that method.


Method Overloading

In method overloading, multiple methods can have the same name with different parameters.

Example :


Java Objects

An object is an instance of a class. Classes and Objects are basic concepts of Object Oriented Programming that revolve around real life entities. Everything in java is associated with classes and Objects.

To create an object of any class, specify the class name, followed by the object name, and use the keyword 'new'.

we can create multiple objects of one class.

Java Class

Class is a template for Objects. when the individual objects are created ,they inherited all the variables and methods from the class.

Example :

Class Objects

Subjects English

Mathematics

Science

Example : Here is the example for Class and Objects

Java Constructors

A constructor in java is a special method that is used to initialize objects.

constructor is similar to a method that is invoked when an object of the class is created.

Unlike Java methods, a constructor has the same name as that of the class and does not have any return type.

Example : Constructor without Parameters


Constructor with Parameters


Java Modifiers

A modifier in Java is used to define the accessibility and behaviors of the classes, their constructors, fields, and methods.

Java has two types of modifiers: Access modifiers and Non-access modifiers.

Access modifiers:

Access modifiers are object-oriented programming that is used to set the accessibility of classes, constructors, methods, and other members of Java.

It will control the access level.

Modifier

Description

public

code is accessible for all classes

private

code is only accessible within the declared class

default

code is only accessible in same package.

protected

code is accessible in same package and subclasses

Non-Access modifiers:

It will not control the access level, but provides the other functionality.

Modifier

Description

final

cannot be overriden/modified

static

used to declare class level variables. static method can call without an object

abstract

can only be used in abstract class.can only used on method which does not have a body. The body is provided by subclass

transient

When an instance variable is declared as transient, then its value doesn't persist when an object is serialized

Synchronized

Methods can only be accessed by one thread at a time

volatile

Value of an attribute is not catched thread-locally, and is always read from the Main memory

Java Interface

An interface is a completely "abstract class" that is used to group related methods with empty bodies.

Another way to achieve abstraction in Java, is with interfaces.

Example : Sigle Interface with two implements

Multiple Interfaces



Java Enums

An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables).

To create an enum , use the enum keyword (instead of class or interface), and separate the constants with a comma. Note that they should be in uppercase letters.

Example :

Output :


ArrayList

ArrayList provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. While elements can be added and removed from an ArrayList whenever you want. ArrayList has normal array inside. If any element added then will store it in the array. it will keep on changing for array is not big enough. It will delete the old array and then create a new array.

Example :

Output :


LinkedList

LinkedList class is almost identical to the ArrayList. LinkedList class has all of the same methods as the ArrayList class because they both implement the List interface. LinkedList has a container inside. The list has a link to the first container and each container has a link to the next container in the list.

Example :

Output :



Java HashMap

HashMap however, store items in "key/value" pairs, and you can access them by an index of another type (e.g. a String).

It can store different types: String keys and Integer values, or the same type, like: String keys and String values:

To access a value in a HashMap, we must know its key. HashMap uses a technique called Hashing. Hashing is a technique of converting a large String to a small String that represents the same String so that the indexing and search operations are faster. HashSet also uses HashMap internally.

Example :

Output :


Java HashSet

HashSet is a collection of items where every item is unique. objects that we insert into the HashSet do not guarantee to be inserted in the same order. The objects are inserted based on their hash code. This class also allows the insertion of NULL elements.

Example :

Output :


Java Iterator

An Iterator is an object that can be used to loop through collections, like ArrayList and HashSet. It is called an "iterator" because "iterating" is the technical term for looping.

Example :


Output :


Conclusion:

Java is a case-sensitive language, which means in code Names and names are two different variables. Rules and syntax of Java are based on the C and C++ languages.

I believe that you have learnt about Most Of the Java Basics and how to use that through this and my previous blogs. Thank You For reading my blog.


22 views0 comments

Recent Posts

See All
bottom of page