Found 9326 Articles for Object Oriented Programming

How to create an Array in Java?

Swarali Sree
Updated on 19-Feb-2020 10:03:30

2K+ Views

In Java, you can create an array just like an object using the new keyword. The syntax of creating an array in Java using new keyword −type[] reference = new type[10];Where, type is the data type of the elements of the array.reference is the reference that holds the array.And, if you want to populate the array by assigning values to all the elements one by one using the index −reference [0] = value1; reference [1] = value2;For example, if you want to create an integer array of 5 elements you can create it using new keyword −int[] myArray = new int[5]; You ... Read More

Does a constructor have a return type in Java?

Monica Mona
Updated on 30-Jul-2019 22:30:20

4K+ Views

No, constructor does not have any return type in Java. Constructor looks like method but it is not. It does not have a return type and its name is same as the class name. Mostly it is used to instantiate the instance variables of a class. If the programmer doesn’t write a constructor the compiler writes a constructors on his behalf. Example If you closely observe the declaration of the constructor in the following example it just have the name of the constructor which is similar to class and, the parameters. It does not have any return type. public ... Read More

What is the character wrapper class and its methods in Java?

Syed Javed
Updated on 30-Jul-2019 22:30:22

5K+ Views

The Character class of the java.lang package wraps a value of the primitive datatype char. It offers a number of useful class (i.e., static) methods for manipulating characters. You can create a Character object with the Character constructor. Character ch = new Character('a'); Following are the notable methods of the Character class. 1 isLetter() Determines whether the specified char value is a letter. 2 isDigit() Determines whether the specified char value is a digit. 3 isWhitespace() Determines whether the specified char value is white space. 4 isUpperCase() Determines ... Read More

Should a constructor always have the same name as the class in java?

Vikyath Ram
Updated on 30-Jul-2019 22:30:22

2K+ Views

Yes, the constructor should always have the same name as the class. Constructor looks like method but it is not. It does not have a return type and its name is same as the class name. Mostly it is used to instantiate the instance variables of a class. If the programmer doesn’t write a constructor the compiler writes a constructors on his behalf. Example Live Demo public class Sample{ public Sample(){ System.out.println("This is a constructor"); } public static void main(String args[]){ Sample obj = new Sample(); } } Output This is a constructor

Can a class in Java be both final and abstract?

Samual Sam
Updated on 30-Jul-2019 22:30:20

1K+ Views

An abstract cannot be instantiated. Therefore to use an abstract class you need to create another class and extend the abstract class and use it. If a class is final you can’t extend it further. So, you cannot declare a class both final and abstract. Example Still if you try to do so you will get a compile time error saying “illegal combination of modifiers:” final abstract class Demo{ public final void display(){ System.out.println("Hello welcome to tutorialspoint"); } } Output C:\Sample>javac Demo.java Demo.java:1: error: illegal ... Read More

What is the number wrapper class and its methods in Java?

vanithasree
Updated on 30-Jul-2019 22:30:20

283 Views

The Number class (abstract) of the java.lang package represents the numeric values that are convertible to primitive types byte, double, float, int, long, and short. Following are the method provided by the Number class of the java.lang package. Sr.No Method & Description 1 byte byteValue() This method returns the value of the specified number as a byte. 2 abstract double doubleValue() This method returns the value of the specified number as a double. 3 abstract float floatValue() This method returns the value of the specified number as a float. ... Read More

What is a method signature in Java?

Swarali Sree
Updated on 30-Jul-2019 22:30:20

6K+ Views

The method signature consists of the method name and the parameter list. Example Live Demo public class MethodSignature { public int add(int a, int b){ int c = a+b; return c; } public static void main(String args[]){ MethodSignature obj = new MethodSignature(); int result = obj.add(56, 34); System.out.println(result); } } Output 90 Method ... Read More

How to create and write JSON array to a file in java?

Abhinaya
Updated on 19-Feb-2020 12:26:38

3K+ Views

Java provides javax.json.Json package which contains classes to read a JSON array:Exampleimport java.io.FileOutputStream; import javax.json.Json; import javax.json.JsonArray; import javax.json.JsonWriter; public class JSONArrayToFile {    public static void main(String args[]) throws Exception {       JsonArray value = Json.createArrayBuilder()          .add(Json.createObjectBuilder()          .add("id", "1001")          .add("Technology", "JavaFX"))          .add(Json.createObjectBuilder()          .add("id", "1002")          .add("Technology", "OpenCV"))          .build();       System.out.println(value);       JsonWriter writer = Json.createWriter(new FileOutputStream("sampleData"));       writer.writeArray(value);       writer.close();    } }Output["JavaFX","HBase","JOGL","WebGL"] After deleting ::["JavaFX","HBase","JOGL"]

What is the super class of every class in Java?

radhakrishna
Updated on 30-Jul-2019 22:30:20

4K+ Views

The class named Object is the super class of every class in Java. Let’s test it with an example. The java.lang.Class.getSuperclass() returns the Class representing the superclass of the entity (class, interface, primitive type or void) represented by this Class. So, Create a sample concrete class and lets try to get the name of its super class using this method. Example Live Demo public class Test { public static void main(String args[]){ Test obj = new Test(); Class cls = obj.getClass().getSuperclass(); ... Read More

How to remove a specific element from a JSON Array in Java?

Govinda Sai
Updated on 19-Feb-2020 12:25:46

13K+ Views

You can remove an element from the JSONArray object using the remove() method. This method accepts an integer and removes the element in that particular index.Exampleimport org.json.JSONArray; public class RemoveFromJsonArray {    public static void main(String args[]) throws Exception {       String [] myArray = {"JavaFX", "HBase", "JOGL", "WebGL"};       JSONArray jsArray = new JSONArray();       for (int i=0; i < myArray.length; i++) {          jsArray.put(myArray[i]);      }      System.out.println(jsArray);      jsArray.remove(3);      System.out.println("After deleting ::"+jsArray);    } }Output["JavaFX","HBase","JOGL","WebGL"] After deleting ::["JavaFX","HBase","JOGL"]

Advertisements