Implement JSON Array Using Streaming API in Java

raja
Updated on 18-Feb-2020 10:47:33

1K+ Views

The JsonGenerator interface can be used to write the JSON data to an output source in a streaming way. We can create or implement a JSON array using the writeStartArray() method of JsonGenerator, this method writes the JSON name/start array character pair within the current object context. The writeStartObject() method writes the JSON start object character, and valid only in an array context and the writeEnd() method writes the end of the current context.SyntaxJsonGenerator writeStartArray(String name)Exampleimport java.io.*; import javax.json.*; import javax.json.stream.*; public class JsonGeneratorTest {    public static void main(String[] args) throws Exception {       StringWriter writer = new ... Read More

Why Java is Not a Pure Object-Oriented Programming Language

varun
Updated on 18-Feb-2020 10:46:57

4K+ Views

A fully object-oriented language needs to have all the 4 oops concepts. In addition to that, all predefined and, user-defined types must be objects and, all the operations should be performed only by calling the methods of a class.Though java follows all the four object-oriented concepts,Java has predefined primitive data types (which are not objects).You can access the members of a static class without creating an object of it.Therefore, Java is not considered as fully object-oriented Technology.

What is a ClassLoader in Java

Prabhas
Updated on 18-Feb-2020 10:46:23

345 Views

Java Virtual machine is the program which accepts .class files as input and converts this to system executable code.The class loader is a module in JVM it loads, links and, initialize a program.Loads the class into the memory.Verifies the byte code instructions.Allocates memory for the program.

Add Leading Zeros to Python Program String

Pradeep Elance
Updated on 18-Feb-2020 10:29:31

285 Views

We may sometimes need to append zeros as string to various data elements in python. There may the reason for formatting and nice representation or there may be the reason for some calculations where these values will act as input. Below are the methods which we will use for this purpose.Using format()Here we take a DataFrame and apply the format function to the column wher we need to append the zeros as strings. The lambda method is used to apply the function repeatedly.Exampleimport pandas as pd string = {'Column' : ['HOPE', 'FOR', 'THE', 'BEST']} dataframe=pd.DataFrame(string) print("given column is ") print(dataframe) ... Read More

Add Image on a Python Tkinter Button

Pradeep Elance
Updated on 18-Feb-2020 10:23:22

2K+ Views

Tkinter, which is the GUI library for python programming has a feature to add images to the GUI buttons. This is useful for users to remember the symbols in the GUI rather than the text in the GUI. In the below Tkinter program we show how we can add an image to a GUI button. The photoimage method from the imageKT module is used. We mention the local path to the image file.Examplefrom tkinter import * from PIL import ImageTk ,Image base = Tk() base.title('Start Button') img=ImageTk.PhotoImage(Image.open ("D:\button.jpg")) lab=Label(image=img) lab.pack() button=Button(base, text='exit', command=base.quit) button.pack() base.mainloop()OutputRunning the above ... Read More

Sort a JSONObject in Java

raja
Updated on 18-Feb-2020 10:07:15

6K+ Views

A JSONObject is an unordered collection of a key, value pairs, and the values can be any of these types like Boolean, JSONArray, JSONObject, Number and String. The constructor of a JSONObject can be used to convert an external form JSON text into an internal form whose values can be retrieved with the get() and opt() methods or to convert values into a JSON text using the put() and toString() methods.In the below example, we can sort the values of a JSONObject in the descending order.Exampleimport org.json.*; import java.util.*; public class JSonObjectSortingTest {    public static void main(String[] args) {       ... Read More

What are Vararg Methods in Java

Lakshmi Srinivas
Updated on 18-Feb-2020 10:02:14

269 Views

In Java methods, parameters accept arguments with three dots. These are known as variable arguments.sample(int args …){}If they are used you can pass a different number of arguments each time you call these methods.Examplepublic class Sample {    void demoMethod(String... args) {       for (String arg: args) {          System.out.println(arg);       }    }    public static void main(String args[] ) {       new Sample().demoMethod("ram", "rahim", "robert");       new Sample().demoMethod("krishna", "kasyap")    } }Outputram rahim robert krishna kasyap

Access Private Methods of a Class from Outside in Java

karthikeya Boyini
Updated on 18-Feb-2020 10:00:48

7K+ Views

You can access the private methods of a class using java reflection package.Step1 − Instantiate the Method class of the java.lang.reflect package by passing the method name of the method which is declared private.Step2 − Set the method accessible by passing value true to the setAccessible() method.Step3 − Finally, invoke the method using the invoke() method.Exampleimport java.lang.reflect.Method; public class DemoTest {    private void sampleMethod() {       System.out.println("hello");    } } public class SampleTest {    public static void main(String args[]) throws Exception {       Class c = Class.forName("DemoTest");       Object obj = c.newInstance(); ... Read More

Ways to Get the Instance of a Class in Java

Monica Mona
Updated on 18-Feb-2020 09:59:52

595 Views

You can create an object of the class named Class in two ways −Using new keyword as −Class obj = new Class();Using the forName() method of the class named Class.Class obj = Class.forName("DemoTest");

Upcasting and Downcasting in Java

karthikeya Boyini
Updated on 18-Feb-2020 09:57:00

2K+ Views

Typecasting is converting one data type to another.Up-casting − Converting a subclass type to a superclass type is known as up casting.Exampleclass Super {    void Sample() {       System.out.println("method of super class");    } } public class Sub extends Super {    void Sample() {       System.out.println("method of sub class");    }        public static void main(String args[]) {       Super obj =(Super) new Sub(); obj.Sample();    } }Down-casting − Converting a superclass type to a subclass type is known as downcasting.Exampleclass Super {    void Sample() {     ... Read More

Advertisements