Object Oriented Programming Articles

Page 487 of 589

Can we define a class inside a Java interface?

V Jyothi
V Jyothi
Updated on 20-Feb-2020 7K+ Views

Yes, you can define a class inside an interface. In general, if the methods of the interface use this class and if we are not using it anywhere else we will declare a class within an interface.Exampleinterface Library { void issueBook(Book b); void retrieveBook(Book b); public class Book { int bookId; String bookName; int issueDate; int returnDate; } } public class Sample implements Library ...

Read More

What is the Eclipse keyboard shortcut for "public static void main(String[] args) " in Java?

Ramu Prasad
Ramu Prasad
Updated on 20-Feb-2020 9K+ Views

To get public static void main(String[] args) line in eclipse without typing the whole line type main and press Ctrl + space then, you will get the option for the main method select it.

Read More

How to find package explorer in Java eclipse project?

V Jyothi
V Jyothi
Updated on 20-Feb-2020 25K+ Views

To view the project explorer, click on Window menu then, click on Show View and select Project Explorer.There is simpler way to open project explorer, when you are in the editor press alt + shift + w and select project explorer.

Read More

How to count the number of lines in a text file using Java?

Arushi
Arushi
Updated on 20-Feb-2020 2K+ Views

To count the number of lines in a fileInstantiate the FileInputStream class by passing an object of the required file as parameter to its constructor.Read the contents of the file to a bytearray using the read() method of FileInputStream class.Instantiate a String class by passing the byte array obtained, as a parameter its constructor.Now, split the above string into an array of strings using the split() method by passing the regular expression of the new line as a parameter to this method.Now, find the length of the obtained array.Exampleimport java.io.File; import java.io.FileInputStream; public class NumberOfCharacters { public ...

Read More

How to count the number of words in a text file using Java?

Paul Richard
Paul Richard
Updated on 20-Feb-2020 6K+ Views

Read the number of words in text fileCreate a FileInputStream object by passing the required file (object) as a parameter to its constructor.Read the contents of the file using the read() method into a byte array. Insatiate a String class by passing the byte array to its constructor.Using split() method read the words of the String to an array.Create an integer variable, initialize it with 0, int the for loop for each element of the string array increment the count.Exampleimport java.io.File; import java.io.FileInputStream; public class Sample {    public static void main(String args[]) throws Exception{       int count =0;       File ...

Read More

What are classpath of projects in Java eclipse projects?

Nikitha N
Nikitha N
Updated on 20-Feb-2020 4K+ Views

You can include Jar files to which you need to set the classpath in the eclipse project using build pathStep 1 − Right click on the project Select Build Path → Configure Build Path.Step 2 − Select libraries select Add External JARs… button.Step3 − Then browse through the folder where the required jar files exits, select them and press open.Selected jar files will be added to the Libraries. Finally, press OK.Now, if you open the Referenced libraries in the project you can observe the added jar file.

Read More

How to count the number characters in a Java string?

Vikyath Ram
Vikyath Ram
Updated on 20-Feb-2020 863 Views

Declare an integer, initialize it with 0, in for loop increment it for each character. Example public class Sample {    public static void main(String args[]) {       String str = new String("Hi welcome to Tutorialspoint");       int count = 0;       for(int i = 0; i<str.length(); i++) {          count++;       }       System.out.println("Number characters in the given string (including spaces) "+count);    } } Output Number characters in the given string (including spaces) 28

Read More

What is the difference between String s1 = "Hello" and String s1= new String("Hello") in java?

Rishi Raj
Rishi Raj
Updated on 19-Feb-2020 4K+ Views

When you store a String asString str1 = "Hello";directly, then JVM creates a String object with the given value in a separate block of memory known as String constant pool.And whenever we try to create another String asString str2 = "Hello";JVM verifies whether any String object with the same value exists in the String constant pool, if so, instead of creating a new object JVM assigns the reference of the existing object to the new variable.And when we store String asString str = new String("Hello");using the new keyword, a new object with the given value is created irrespective of the ...

Read More

What does the modifier volatile in Java do?

Jai Janardhan
Jai Janardhan
Updated on 19-Feb-2020 712 Views

The volatile modifier is used to let the JVM understand that a thread accessing the variable should always merge its own personal copy of the variable with the original in the memory.Accessing a volatile variable synchronizes all the cached copy of the variables in the main memory. Volatile can only be applied to instance variables, which are of type object or private. A volatile object reference can be null.Examplepublic class MyRunnable implements Runnable { private volatile boolean active; public void run() { active = true; while (active) { } } public void stop() { active = false; } }

Read More

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

Govinda Sai
Govinda Sai
Updated on 19-Feb-2020 15K+ 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"]

Read More
Showing 4861–4870 of 5,881 articles
« Prev 1 485 486 487 488 489 589 Next »
Advertisements