الأحد، 19 نوفمبر 2017

Encapsulation in Java with Examples

What is Encapsulation in Java 

Encapsulation is one of the principle of OOPs.

Encapsulation describes the ability of hiding data and methods of object.
Encapsulation is a process of arranging information about data and behavior into a single component like as object in Java.

Keeping all members and methods are within bounds of class. In Java, all class are under encapsulated.

If we set all fields of class as private, so no other code outside of this class can not access private variables. To access these private variables, we have to declare public methods in class.


So these private members are not accessible by outside objects or classes.

A class can have authority to modify the values using setter and getter methods of class. By using getter methods, we can make read-only and wise versa for write-only.

الأربعاء، 15 نوفمبر 2017

3 Ways to Find Sum of First n numbers program in Java

In this post, You will learn today today how to find sum of first n numbers in java using while loop, for loop and mathematics formula(Optimized).

For this program, we have to input the natural number n value which you want to get the sum. So that Our program will get sum of first n numbers. This is basic program for freshers or who are studying engineering in their lab programs.



Example 1:
number = 10
sum = 55

Example 2:
number = 100
sum = 5050

الجمعة، 10 نوفمبر 2017

Java Supports Pass By Value only, not Pass By Ref

Does Java supports Pass By Value or Pass By Reference:

 

Very common question in java interviews for freshers and experienced. But, this is very tricky and confusion for most of the people whether java is pass by value or pass by reference.

First, we will make clear what is call by value and call by reference. These two comes into play when we pass parameters in invoking a method.

java-pass-by-value




Call By Value: Values only passed as copy of original. But not original memory location. It creates a new copy in the memory and passes to the method that we invoked.

Call By Reference: Passes reference or alias of the object which is said to be Call By Reference. Unfortunately, Java inventors decided to call the location of an object as a "reference". When we pass the value of an object, we are passing the reference to it. This is confusing to beginners.

الخميس، 9 نوفمبر 2017

Iterating String Array Examples in Java - Iterate Through String Array in Java

For-Each Example: Enhanced for Loop to Iterate Java Array:

How to iterate String array in Java. This is a common scenario which is being used in our projects.
Let us take a string array example that we want to iterate over it.

Note: Array index always starts from 0 programmatically.


String Array Iterating:

A String Array is a data structure that holds several String values. The number of String elements is fixed and can't be changed. Iterating over an array means accessing each element of array one by one. There may be many ways of iterating over an array in Java, below are some simple ways. 

We can do this in mainly three ways.

Step 1): Using for loop
Step 2): Using while loop
Step 3): Using For-Each loop


Iterating String Array Examples in Java


السبت، 4 نوفمبر 2017

Java Program to Add Three Numbers (With Possible Runtime Exception)

1. Overview:


In this post, we will learn how to add three numbers in java with simple example program. We will explain step by step explanation.

In mathematics, the summation is calculated by using '+' [plus] operator. We will achieve the sum of three numbers using '+' operator in java.

Sum Of Three Numbers in Java

Formula:


sum = a + b + c;