Found 2616 Articles for Java

How to Write/create a JSON file using Java?

Anvi Jain
Updated on 06-Sep-2023 13:25:18

48K+ Views

JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. Conventions used by JSON are known to programmers, which include C, C++, Java, Python, Perl, etc. Sample JSON document −{    "book": [       {          "id": "01",          "language": "Java",          "edition": "third",          "author": "Herbert Schildt"       },       {          "id": "07",          "language": "C++",          "edition": "second",          "author": "E.Balagurusamy" ... Read More

How to read/parse JSON array using Java?

Nishtha Thakur
Updated on 31-Oct-2023 14:38:33

51K+ Views

A Json array is an ordered collection of values that are enclosed in square brackets i.e. it begins with ‘[’ and ends with ‘]’. The values in the arrays are separated by ‘, ’ (comma).Sample JSON array{    "books": [ Java, JavaFX, Hbase, Cassandra, WebGL, JOGL] }The json-simple is a light weight library which is used to process JSON objects. Using this you can read or, write the contents of a JSON document using Java program.JSON-Simple maven dependencyFollowing is the maven dependency for the JSON-simple library −           com.googlecode.json-simple       json-simple       ... Read More

How to read the contents of a JSON file using Java?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

8K+ Views

JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. Conventions used by JSON are known to programmers, which include C, C++, Java, Python, Perl, etc. Sample JSON document −{    "book": [       {          "id": "01",          "language": "Java",          "edition": "third",          "author": "Herbert Schildt"       },       {          "id": "07",          "language": "C++",          "edition": "second",          "author": "E.Balagurusamy" ... Read More

Is Swing thread-safe in Java?

raja
Updated on 07-Feb-2020 06:16:23

1K+ Views

No, Java Swing components are not thread-safe in Java.Why Swing Components are not thread-safeOne of the main reason for Java Swing is not thread-safe is to simplify the task of extending its components.Another reason for the Java Swing is not thread-safe due to the overhead involved in obtaining and releasing locks and restoring the state.Some of the Java Swing component methods will support multi-threaded access like repaint(), revalidate(), and invalidate() methods of JComponent class.Event Dispatch Thread (EDT)The Java Swing components can only be accessed from the Event Dispatch Thread (EDT) once a component is available for painting onscreen. The EDT thread is the thread that ... Read More

How can we create a login form in Java?

raja
Updated on 07-Feb-2020 06:20:10

24K+ Views

We can develop a login form in Java using Java Swing technology. In this example, we can create two labels username and password, two text fields for the user to enter valid credentials and finally one submit button. Once the user is able to enter the valid credentials in the two text fields, we can able to see Hello admin in the login form.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class LoginDemo extends JFrame implements ActionListener {    JPanel panel;    JLabel user_label, password_label, message;    JTextField userName_text;    JPasswordField password_text;    JButton submit, cancel;    LoginDemo() {       // Username Label       ... Read More

When will be an IllegalStateException (unchecked) thrown in Java?

raja
Updated on 07-Feb-2020 06:21:13

286 Views

An IllegalStateException is an unchecked exception in Java. This exception may arise in our java program mostly if we are dealing with the collection framework of java.util.package. There are many collections like List, Queue, Tree,  Map out of which List and Queues (Queue and Deque) to throw this IllegalStateException at particular conditions.When will be IllegalStateException is thrownAn IllegalStateExceptionexception will be thrown, when we try to invoke a particular method at an inappropriate time.In case of java.util.List collection, we use next() method of the ListIterator interface to traverse through the java.util.List. If we call the remove() method of the ListIterator interface before calling the next() method then this exception will be thrown as ... Read More

How to create a custom unchecked exception in Java?

raja
Updated on 07-Feb-2020 06:23:44

8K+ Views

We can create the custom unchecked exception by extending the RuntimeException in Java.Unchecked exceptions inherit from the Error class or the RuntimeException class. Many programmers feel that we cannot handle these exceptions in our programs because they represent the type of errors from which programs cannot be expected to recover while the program is running. When an unchecked exception is thrown, it is usually caused by misuse of code,  passing a null or otherwise incorrect argument.Syntaxpublic class MyCustomException extends RuntimeException {    public MyCustomException(String message) {       super(message);    } }Implementing an Unchecked ExceptionThe implementation of a custom unchecked exception is almost similar to a checked ... Read More

Is it possible to assign a reference to "this" in java?

Maruthi Krishna
Updated on 29-Jun-2020 12:21:45

971 Views

The "this" keyword in Java is used as a reference to the current object, within an instance method or a constructor. Using it, you can refer the members of a class such as constructors, variables, and methods.Assigning reference to "this"According to the definition "this" is a keyword which acts as a reference to the current object (the object from whose constructor/method you are using it), its value id is fixed. Therefore, you cannot assign a new reference value to it. Moreover, it is just a keyword, not a variable.Still, if you try to it assign a reference value to "this" it ... Read More

Explain the basic structure of a program in Java?

raja
Updated on 07-Feb-2020 05:37:58

4K+ Views

A typical structure of a Java program contains the following elementsPackage declarationImport statementsCommentsClass definitionClass variables, Local variablesMethods/BehaviorsPackage declarationA class in Java can be placed in different directories/packages based on the module they are used. For all the classes that belong to a single parent source directory, a path from source directory is considered as package declaration.Import statementsThere can be classes written in other folders/packages of our working java project and also there are many classes written by individuals, companies, etc which can be useful in our program. To use them in a class, we need to import the class that we intend ... Read More

What is the order of execution of non-static blocks with respect to a constructor in Java?

raja
Updated on 29-Jun-2020 12:06:53

2K+ Views

Whenever an object is created, a non-static block will be executed before the execution of the constructor.Non-Static BlocksThe Non-static blocks are class level blocks which do not have any prototype.The need for a non-static block is to execute any logic whenever an object is created irrespective of the constructor.The Non-static blocks are automatically called by the JVM for every object creation in the java stack area.We can create any number of Non-static blocks in Java.The order of execution of non-static blocks is an order as they are defined.ExampleLive Demopublic class NonStaticBlockTest {    {       System.out.println("First Non-Static Block"); // first non-static ... Read More

Advertisements