Found 9320 Articles for Object Oriented Programming

How to declare a class in Java?

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

12K+ Views

Following is the syntax to declare a class. class className { //Body of the class } You can declare a class by writing the name of the next to the class keyword, followed by the flower braces. Within these, you need to define the body (contents) of the class i.e. fields and methods.To make the class accessible to all (classes) you need to make it public. public class MyClass { //contents of the class (fields and methods) }

How to convert a byte array to a hex string in Java?

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

380 Views

The printHexBinary() method of the DatatypeConverter class accepts a byte array and returns a hex string.Exampleimport javax.xml.bind.DatatypeConverter; public class ByteToHexString { public static void main(String args[]) { String sam = "Hello how are you how do you do"; byte[] byteArray = sam.getBytes(); String hex = DatatypeConverter.printHexBinary(byteArray); System.out.println(hex); } }Output48656C6C6F20686F772061726520796F7520686F7720646F20796F7520646F

What are Java classes?

Giri Raju
Updated on 30-Jul-2019 22:30:20

537 Views

A class in Java is a user-defined datatype, a blueprint, a classification, that describes the behavior/state that the object of its type support. Example public class Dog { String breed; int age; String color; void barking() { } void hungry() { } void sleeping() { } } A class can contain any of the following variable types. Local variables − Variables defined inside methods, constructors or blocks are called local ... Read More

How to convert BLOB to Byte Array in java?

Ramu Prasad
Updated on 30-Jul-2019 22:30:20

6K+ Views

You can contents of a blob into a byte array using the getBytes() method.Exampleimport java.awt.Image; import java.awt.image.BufferedImage; import java.sql.Blob; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Arrays; public class BlobToByteArray { public static void main(String[] args) throws Exception { Image image = new BufferedImage(300, 400, BufferedImage.TYPE_INT_RGB); String JDBC_DRIVER = "com.mysql.jdbc.Driver"; String DB_URL = "jdbc:mysql://localhost/mydb"; String USER = "root"; String PASS = "password"; ... Read More

How to Map multi-dimensional arrays to a single array in java?

V Jyothi
Updated on 19-Feb-2020 12:15:46

2K+ Views

A two-dimensional array is nothing but an array of one dimensional arrays. Therefore to map a two dimensional array into one dimensional arrays.Create arrays equal to the length of the 2d array and, using for loop store the contents of the 2d array row by row in the arrays created above.Examplepublic class Mapping_2DTo1D {    public static void main(String args[]) {       int [][] array2D = {{7, 9, 8, 5}, {4, 5, 1, 8}, {9, 3, 2, 7}, {8, 1, 0, 9}};       int [] myArray1 = new int[array2D[0].length];       int [] myArray2 = ... Read More

How (where) are the elements of an array stored in memory?

Priya Pallavi
Updated on 30-Jul-2019 22:30:20

1K+ Views

In Java, arrays are objects, therefore just like other objects arrays are stored in heap area. An array store primitive data types or reference (to derived data) types Just like objects the variable of the array holds the reference to the array.

Can i refer an element of one array from another array in java?

Nikitha N
Updated on 16-Jun-2020 10:04:41

392 Views

Yes, you can −int [] myArray1 = {23, 45, 78, 90, 10}; int [] myArray2 = {23, 45, myArray1[2], 90, 10};But, once you do so the second array stores the reference of the value, not the reference of the whole array. For this reason, any updating in the array will not affect the referred value −ExampleLive Demoimport java.util.Arrays; public class RefferencingAnotherArray {    public static void main(String args[]) {       int [] myArray1 = {23, 45, 78, 90, 10};       int [] myArray2 = {23, 45, myArray1[2], 90, 10};       System.out.println("Contents of the ... Read More

What are variable length (Dynamic) Arrays in Java?

Srinivas Gorla
Updated on 19-Feb-2020 12:12:03

4K+ Views

In Java, Arrays are of fixed size. The size of the array will be decided at the time of creation. But if you still want to create Arrays of variable length you can do that using collections like array list.Exampleimport java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class AddingItemsDynamically {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter the size of the array :: ");       int size = sc.nextInt();       String myArray[] = new String[size];       System.out.println("Enter elements of the array (Strings) :: ");       for(int i=0; i

How to create a generic array in java?

Abhinanda Shri
Updated on 30-Jul-2019 22:30:20

171 Views

No, we can’t create generic arrays in java.

How to add items to an array in java dynamically?

Ankitha Reddy
Updated on 30-Jul-2019 22:30:20

9K+ Views

Since the size of an array is fixed you cannot add elements to it dynamically. But, if you still want to do it then, Convert the array to ArrayList object.Add the required element to the array list.Convert the Array list to array.Exampleimport java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class AddingItemsDynamically {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter the size of the array :: ");       int size = sc.nextInt();       String myArray[] = new String[size];       System.out.println("Enter elements of the array ... Read More

Advertisements