Syed Javed has Published 46 Answers

Palindrome program in Java.

Syed Javed

Syed Javed

Updated on 18-Jun-2020 07:05:31

Following is the required program.ExampleLive Demopublic class Tester {    public static void main(String args[]) {       int r, sum = 0, temp;       int m = 454;       int n = m;       temp = n;       while (n ... Read More

Binary search in Java.

Syed Javed

Syed Javed

Updated on 18-Jun-2020 06:48:51

Following is the required program.ExampleLive Demopublic class Tester {    public static int binarySearch(int arr[], int first, int last, int element){       int mid = (first + last)/2;       while( first

Fibonacci series program in Java without using recursion.

Syed Javed

Syed Javed

Updated on 18-Jun-2020 06:48:02

Following is the required program.ExampleLive Demopublic class Tester {    public static void main(String args[]) {        int n1 = 0, n2 = 1, n3, i, max = 5;        System.out.print(n1 + " " + n2);        for (i = 2; i < max; ++i) {           n3 = n1 + n2;           System.out.print(" " + n3);           n1 = n2;           n2 = n3;        }     }  }Output0 1 1 2 3

What are the methods of an array object in JavaScript?

Syed Javed

Syed Javed

Updated on 18-Jun-2020 06:46:54

The Array object lets you store multiple values in a single variable. It stores a fixed-size sequential collection of elements of the same type.Here is a list of some of the methods of the Array object:S.NoMethod & Description       1concat()Returns a new array comprised of this array joined with ... Read More

Java do-while loop example

Syed Javed

Syed Javed

Updated on 12-Mar-2020 12:20:11

A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.SyntaxFollowing is the syntax of a do...while loop −do {    // Statements }while(Boolean_expression);Notice that the Boolean expression appears at the end of the loop, so the statements in ... Read More

Local variables in Java

Syed Javed

Syed Javed

Updated on 12-Mar-2020 12:14:34

Local variables are declared in methods, constructors, or blocks.Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or block.Access modifiers cannot be used for local variables.Local variables are visible only within the declared method, constructor, ... Read More

How is down-casting possible in Java?

Syed Javed

Syed Javed

Updated on 12-Mar-2020 10:34:57

Yes, a variable can be downcast to its lower range substitute by casting. It may lead to data loss although. See the example below −ExampleLive Demopublic class Tester {      public static void main(String[] args) {       int a = 300;         byte b = (byte)a;         System.out.println(b);    }   }OutputIt will print output as −44

How to use for each loop through an array in Java?

Syed Javed

Syed Javed

Updated on 12-Mar-2020 10:20:25

JDK 1.5 introduced a new for loop known as foreach loop or enhanced for loop, which enables you to traverse the complete array sequentially without using an index variable.ExampleLive Demopublic class ArrayUsingForEach {    public static void main(String[] args) {       double[] myList = {1.9, 2.9, 3.4, 3.5};       for (double element: myList) {          System.out.println(element);       }    } }Output1.9 2.9 3.4 3.5

3 ways to initialize an object in Java

Syed Javed

Syed Javed

Updated on 06-Mar-2020 06:09:29

Consider a class Tester which has implemented Cloneable interface. Now you can initialize an object using following three ways −1. Using new keyword.Tester tester1 = new Tester();2. Using Class.forName() methodTester tester4 = (Tester)Class.forName("Tester").newInstance();3. Using clone method.Tester tester2 = tester1.clone();

Instance variable in Java

Syed Javed

Syed Javed

Updated on 06-Mar-2020 05:41:51

Instance VariablesEach object has its unique set of instance variables. An object's state is created by the values assigned to these instance variables.ExampleLive Demopublic class Tester {    private String message;    public Tester(String message){       this.message = message;    }    public void printMessage(){       ... Read More

Advertisements