DEV Community

loading...
Cover image for You Must Know This About Java - II

You Must Know This About Java - II

Ritvik Dubey
I'm a front-end web developer.
・4 min read

This article is remaining half of my last article, it would be better if you do check this one before continuing (in last article I wrote about what is Java how it works etc) You Must Know This About Java - I, in this I'll conclude the other remaining basic terms of Java making it as short as possible.

Let's begin...

Let's talk about Java code

So Java code is a well organized code. Java code must be written in Class or Interface or in enum body. Writing code in any of these bodies gives security and connectivity or mapping [one to one, one to many, many to one, many to many]. When developing applications in Java, hundreds of classes and interfaces will be written.

  • Java is case sensitive
  • Java is strongly typed
  • White-spaces are ignored by compiler
  • Unlike JavaScript and Python you have to take care of semicolons (;) in Java.

Object

object.jpg
Look around and you'll find many examples of real-world objects like your car, your computer system, your bag etc. They all have state and behavior. Car have state(fuel level, wheels, gears etc) and behavior(accelerating, changing gear etc). Objects have states and behaviors. Object's state is stored in fields(variables) and behavior is shown via methods.

Class

class.jpg
When you look around you'll find many individual objects of same kind for example BMW, Audi, Mercedes etc all are car, it means they all have 4 wheels, gears etc that means they all are built from the same set of blueprints and therefore contains the same components. A class is the blueprint from which individual objects are created. A class can have any number of methods.

This will be more clear from this picture:-
class-object.jpg

Object class

Object class is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class. If a Class does not extend any other class then it is direct child class of Object and if extends other class then it is an indirectly derived. Hence the Object class is the parent class of all the classes in java by default. In other words, it is the topmost class of java.

Runtime class

Java Runtime class is used to interact with java runtime environment. Java Runtime class provides methods to execute a process, invoke GC, get total and free memory etc. There is only one instance of java.lang.Runtime class is available for one java application.

There are 4 different access levels-

  • default - Accessible within same package
  • public - Accessible everywhere
  • private - Accessible within same class
  • protected - Can be accessed after importing

Method

Method represents some specific task in a program. Methods allow us to reuse the code without retyping the code. The most important method in Java is the main() method.
method.jpg

  • Method describes the mechanisms that actually perform its tasks.
  • Method hides from its user the complex tasks that it performs.
  • Method call tells method to perform its task.

Two methods of same class can be called together

Syntax:-
method().method();

Instance Variable

Instance variables is used by Objects to store their states. Instance variables are declared in a class, but outside a method, constructor or any block. They are called so because their values are instance specific and are not shared among instances. Each object have a unique set of instance variable.

Packages

A Package can be defined as a collection of similar types of classes, interfaces and sub-packages in the form of directory structure. In simple words you can say a Java package is used to group related classes together. To use any package import keyword is used. There are two categories of packages, built-in packages and user-defined packages. You can think of it as a folder.
packages.png

Description of package importing -

package-structure.jpeg
Importing a particular class from the package

import java.util.Scanner;
Enter fullscreen mode Exit fullscreen mode

Importing all classes of the package

import java.util.*; //this is bad practice
Enter fullscreen mode Exit fullscreen mode

The Number of words in a package are called identifiers.

Framework

A Framework can be defined as collection of built-in objects and libraries to create any real world application. In other words, Java Framework is a collection of predefined classes and functions that is used to process input, manage hardware devices interacts with system software. It provides more security, more networking features, and no server down on huge traffic.
Java provides us some very strong frameworks -
strong.jpg

  • Spring
  • Hibernate
  • Grails
  • JavaServer Faces(JSF)
  • Struts

Okay so that's enough for now follow my this journey to learn more about Java.

Thank you for reading.

Please share your thoughts about it and correct me if I'm wrong.

I hope you liked it and found it helpful.

Cover:- Rajat Gour

Connect with me on Twitter or LinkedIn

My personal blog blog.ritvikdubey.com

Discussion (0)