Found 4338 Articles for Java 8

Java Programs for printing Pyramid Patterns. (of numbers)

Venkata Sai
Updated on 30-Jul-2019 22:30:26

179 Views

Following is a Java program which prints a pyramid of numbers.Example Live Demopublic class PrintingPyramids {    public static void main(String args[]){       int n,i,j,k=1;       n = 5;       for(i = 0; i

Java Program to find all angles of a triangle

Venkata Sai
Updated on 30-Jul-2019 22:30:26

426 Views

To find the angles of a triangle we can use sin/cosine rules according to cosine rule −cos A = (b^2 + c^2 - a^2)/2bcwhere A, B , C are the vertices and a, b, c are the sides of the given triangle then, angles of the triangle when the sides a, b, c given are −angleAtA = acos((b^2 + c^2 - a^2)/(2bc)) angleAtB = acos((a^2 + c^2 - b^2)/(2ac)) angleAtC = acos((a^2 + b^2 - c^2)/(2ab))

What are the default array values in Java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

2K+ Views

In Java arrays are the reference types which stores multiple elements of the same datatype. You can create an array just like an object using the new keyword −type[] reference = new type[10];or, directly using the flower brackets ({}).int [] myArray = {10, 20, 30, 40, 50}When you create instance variables in Java you need to initialize them, else the compiler will initialize on your behalf with default values.Similarly, if you create an array as instance variable, you need to initialize it else the compiler initializes with default values which are −Integer − 0Byte − 0Float − 0.0Boolean − falseString/Object ... Read More

What is instance variable hiding in Java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

1K+ Views

Whenever you inherit a superclass a copy of superclass’s members is created at the subclass and you using its object you can access the superclass members.If the superclass and the subclass have instance variable of same name, if you access it using the subclass object, the instance variables of the subclass hides the instance variables of the superclass irrespective of the types. This mechanism is known as field hiding or, instance variable hiding.But, since it makes code complicated field hiding is not recommended.ExampleIn the following example we have two classes Super and Sub one extending the other. They both have ... Read More

Is main method compulsory in Java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

3K+ Views

To compile a program, you doesn’t really need a main method in your program. But, while execution JVM searches for the main method. In the Java the main method is the entry point Whenever you execute a program in Java JVM searches for the main method and starts executing from it.The main method must be public, static, with return type void, and a String array as argument.public static int main(String[] args){ }You can write a program without defining a main it gets compiled without compilation errors. But when you execute it a run time error is generated saying “Main method ... Read More

How to convert an object array to an integer array in Java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

5K+ Views

You can convert an object array to an integer array in one of the following ways −By copying each element from integer array to object array −Exampleimport java.util.Arrays; public class ObjectArrayToStringArray {    public static void main(String args[]){       Object[] objArray = {21, 58, 69, 33, 65};       int length = objArray.length;       int intArray[] = new int[length];       for(int i=0; i

Explain narrowing with object in Java.

Venkata Sai
Updated on 30-Jul-2019 22:30:26

2K+ Views

Java provides various datatypes to store various data values. It provides 7 primitive datatypes (stores single values) namely, boolean, byte, char, short, int, long, float, double and, reference datatypes (arrays and objects).Type Casting/type conversion −Converting one primitive datatype into another is known as type casting (type conversion) in Java. You can cast the primitive datatypes in two ways namely, Widening and, Narrowing.Narrowing − Converting a higher datatype to a lower datatype is known as narrowing. In this case the casting/conversion is not done automatically, you need to convert explicitly using the cast operator “( )” explicitly. Therefore, it is known ... Read More

Explain widening with objects in Java.

Venkata Sai
Updated on 30-Jul-2019 22:30:26

2K+ Views

Java provides various datatypes to store various data values. It provides 7 primitive datatypes (stores single values) namely, boolean, byte, char, short, int, long, float, double and, reference datatypes (arrays and objects).Type Casting/type conversion − Converting one primitive datatype into another is known as type casting (type conversion) in Java. You can cast the primitive datatypes in two ways namely, Widening and, Narrowing.Widening − Converting a lower datatype to a higher datatype is known as widening. In this case the casting/conversion is done automatically therefore, it is known as implicit type casting. In this case both datatypes should be compatible ... Read More

Can we cast reference variables in Java?

Venkata Sai
Updated on 06-Aug-2019 12:49:02

1K+ Views

Java provides various datatypes to store various data values. It provides 7 primitive datatypes (stores single values) namely, boolean, byte, char, short, int, long, float, double and, reference datatypes (arrays and objects).Casting in JavaConverting one primitive data type into another is known as type casting.Exampleimport java.util.Scanner; public class TypeCastingExample {    public static void main(String args[]){       Scanner sc = new Scanner(System.in);       System.out.println("Enter an integer value: ");       int i = sc.nextInt();       long num = i;       System.out.println("Value of the given integer: "+num);    } }OutputEnter an integer ... Read More

What is deep copy? Explain with an example in Java.

Venkata Sai
Updated on 30-Jul-2019 22:30:26

6K+ Views

Creating an exact copy of an existing object in the memory is known as cloning.The clone() method of the class java.lang.Object accepts an object as a parameter, creates and returns a copy of it (clones).In order to use this method, you need to make sure that your class implements the Cloneable interface.Example Live Demoimport java.util.Scanner; public class CloneExample implements Cloneable {    private String name;    private int age;    public CloneExample(String name, int age){       this.name = name;       this.age = age;    }    public void displayData(){       System.out.println("Name : "+this.name);     ... Read More

Advertisements