Showing posts with the label Java Stuff

Java Class and Object

Classes and Objects are the fundamental building blocks of OOPS (Object Oriented Programming). A java object is a physical and logical entity where as java class is logical entity. In this tutorial, we will learn about Java Objects and Classes. What is a Class in Java? Before creating an object in Java, you need to define a class. A class is a blueprint from which object is created.  We can think of class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows etc. Based on these descriptions we build the house. House is the object. Since, many houses can be made from the same description, we can create many objects from a class. A class can have following entities inside it: Fields (Variables) Methods Constructor Nested Class Interface Blocks Syntax to define a Java Class: public class Dog { String breed; int age; String color; public void barking () { } public void hungry () { } } Explanation of the example code:

What are Java Methods?

In this post, we'll learn everything about Java Methods. Table of Contents What is a Java Method ? How to Create a Method ? What is a Main method in Java ? Types of Java Methods Parameterized Methods Non - Parameterized Methods How to Call a Method ? Static Methods Non - Static Methods 1. What is a method in Java? A method in java is a group of instructions that are performed together to do a specific task. Methods are generally known as Functions in other languages like C, C++ etc.  Let's say you want to print numbers 1 to 10 in your program multiple times. So, if you have created a method with code which can print 1 to 10 numbers then you can just call that method multiple times in your program and you do not have to write the code to print 1 to 10. Java Methods have some features, let's take a look at them: Java Method can be public, private, protected or default. Java method can or cannot return a value. If you do not want your method to return anything, you have to dec

What are Variables in Java?

In this post you will learn about Java variables. What is a Java Variable? A variable can be called as a container which holds some value in a java program.  Variable has assigned a data type which defines what type of value variable can hold. Variable has a name which locates to the memory. If you want to use variable in a program, there are two steps associated with it you have to perform: Variable Declaration Variable Initialization i. Variable Declaration While declaring a variable you have to assign data type with variable name. Variable name has to be unique. Example: String employee; ii. Variable Initialization It means assigning a value to the declared variable. Example: employee = "John" Above two steps (declaration and initialization) can also be joined together in one single step. Example: String employee = "John"; Let's explain above example,  ' String ' is data type ' empl

Java Constructors

  Constructor in Java In java a constructor is called a special method. It is used to create objects in java. A constructor is called whenever we initialize an object. It has some special features: It has same name as that of a class. It does not have any return type. It's syntax is similar to method. A java constructor cannot be static, final or abstract. A class always have a default constructor whether you have declared it or not. But if you define a custom constructor then default constructor is no longer in use. There are two types of constructors in Java: No-Parameterized Constructor (Default Constructor) Parameterized Constructor No-Parameterised Constructor As the name suggests, this constructor doesn't have any parameter in it's signature. Syntax for Default Constructor: public class MyClass { // Creating a default constructor MyClass(){ System. out . println ( "Object created" ); } // Main method public static void main (String

Java Loops and Control Statements

In programming if you want to repeat the execution of particular set of instructions or want to execute a function/set of lines repeatedly then we use loops. For example you want to print a number 10 times, what would you do? Probably a newbie would write print statement 10 times, but we can do the same thing to use a loop. Let's understand the different types of loops with examples. For loop For-each loop While loop Do-While loop 1. For Loop When you know that you want to execute your code fixed no. of times say 10 times, then for loop comes into picture. It's the mostly used and the simplest loop in any programming. Here is the For loop structure: for (initializing variable; condition; increment/decrement) { // Code/Statement to be iterated } It has four different elements, let's understand them: Initialization Variable - It is the starting point of the for loop. This variable gets initialized when for loop gets started. Condition - It comes into play after initial

Java Conditional Statements

  Java , like all other programming languages, is equipped with specific statements that allow us to check a  condition  and execute certain parts of code depending on whether the  condition  is true or false. Such statements are called conditional, and are a form of composite statement. The Java  if statement  is used to test the condition. It checks boolean condition:  true  or  false . There are various types of if statement in Java. if statement if-else statement if-else-if ladder nested if statement Java supports the usual logical conditions from mathematics: Less than:  a < b Less than or equal to:  a <= b Greater than:  a > b Greater than or equal to:  a >= b Equal to  a == b Not Equal to:  a != b Java if Statement The Java if statement tests the condition. It executes the  if block  if condition is true. Syntax: 1.    if (condition){   2.    //code to be executed    3.    }   Example: 1.    //Java Program to demonstrate t