Java Articles

Page 300 of 450

What happens if try to access an element with an index greater than the size of the array in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 3K+ Views

An array is a data structure/container/object that stores a fixed-size sequential collection of elements of the same type. The size/length of the array is determined at the time of creation.The position of the elements in the array is called as index or subscript. The first element of the array is stored at the index 0 and, the second element is at the index 1 and so on.Each element in an array is accessed using an expression which contains the name of the array followed by the index of the required element in square brackets.For example, if an array of 6 elements ...

Read More

Can you pass the negative number as an Array size in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 3K+ Views

In general, arrays are the containers that store multiple variables of the same datatype. These are of fixed size and the size is determined at the time of creation. Each element in an array is positioned by a number starting from 0.You can access the elements of an array using name and position as −System.out.println(myArray[3]); //Which is 1457Creating an array in JavaIn Java, arrays are treated as referenced types you can create an array using the new keyword similar to objects and populate it using the indices as −int myArray[] = new int[7];While creating array in this way, you must ...

Read More

Can you assign an Array of 100 elements to an array of 10 elements in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 3K+ Views

In general, arrays are the containers that store multiple variables of the same datatype. These are of fixed size and the size is determined at the time of creation. Each element in an array is positioned by a number starting from 0.You can access the elements of an array using name and position as −System.out.println(myArray[3]); //Which is 1457Creating an array in JavaIn Java, arrays are treated as referenced types you can create an array using the new keyword similar to objects and populate it using the indices as −int myArray[] = new int[7];While creating array in this way, you must specify ...

Read More

What are the best practices to keep in mind while using packages in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 1K+ Views

You can create the .class files of all the Java classes and interfaces related to each other in one folder automatically by declaring them under same package. A package is nothing but a directory storing classes and interfaces of a particular concept.Creating a packageYou can create a package and add required classes/interfaces in it just by declaring the package on the top of the Class/Interface files using the keyword package as −Package package_name;ExampleFollowing Java program, demonstrates the declaration of a package in Java.package com.tutorialspoint.mypackage; public class Sample{    public void demo(){       System.out.println("This is a method of the ...

Read More

Difference between import and package in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 3K+ Views

In Java classes and interfaces related to each other are grouped under a package. Package is nothing but a directory storing classes and interfaces of a particular concept. For example, all the classes and interfaces related to input and output operations are stored in java.io package.Creating a packageYou can group required classes and interfaces under one package just by declaring the package at the top of the Class/Interface (file) using the keyword package as −package com.tutorialspoint.mypackage; public class Sample{    public void demo(){       System.out.println("This is a method of the sample class");    }    public static void main(String ...

Read More

How do you print the content of an array in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 601 Views

In general, arrays are the containers that store multiple variables of the same datatype. These are of fixed size and the size is determined at the time of creation. Each element in an array is positioned by a number starting from 0.You can access the elements of an array using name and position as −System.out.println(myArray[3]); //Which is 1457Creating an array in JavaIn Java, arrays are treated as referenced types you can create an array using the new keyword similar to objects and populate it using the indices as −int myArray[] = new int[7]; myArray[0] = 1254; myArray[1] = 1458; myArray[2] ...

Read More

Difference between the byte stream and character stream classes in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 5K+ Views

Java provides I/O Streams to read and write data where, a Stream represents an input source or an output destination which could be a file, i/o devise, other program etc.Based on the data they handle there are two types of streams −Byte Streams − These handle data in bytes (8 bits) i.e., the byte stream classes read/write data of 8 bits. Using these you can store characters, videos, audios, images etc.Character Streams − These handle data in 16 bit Unicode. Using these you can read and write text data only.The Reader and Writer classes (abstract) are the super classes of ...

Read More

Write a program to print message without using println() method in java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 7K+ Views

The println() method of the System class accepts aStringas parameter an prints the given String on the console.Examplepublic class PrintData {    public static void main(String args[]) {       System.out.println("Hello how are you");    } }OutputHello how are youIn addition to this you can print data on the console in several other ways, some of them are −Using Output StreamsUsing output stream classes, you can write dat on the specified destination. You can print data on the screen/console by passing the standard output Stream object System.out as source to them.Exampleimport java.io.IOException; import java.io.OutputStreamWriter; public class PrintData {   ...

Read More

Is it possible to synchronize the string type in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 2K+ Views

A thread is a piece of code (under execution) in a program, which executes a sub task of the process independently. independent process.In other words, a thread is a light weight process which executes a piece of code independently.Thread synchronizationIf a process has multiple threads running independently at the same time (multi-threading) and if all of them trying to access a same resource an issue occurs.To resolve this, Java provides synchronized blocks/ synchronized methods. If you define a resource (variable/object/array) inside a synchronized block or a synchronized method, if one thread is using/accessing it, other threads are not allowed to ...

Read More

Why String class is popular key in a HashMap in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 4K+ Views

A map is a collection in Java which stores key value pairs. The keys of this must not be null and each key should point to only one value. It is represented by the Map interface of java.util package. There are various classes which provides implementation to this interface.The HashMap is a class which implements the Map interface. It is based on the Hash table. It allows null values and null keys.In short, you can store key value pairs in the HashMap object. Once you do so you can retrieve the values of the respective keys but, the values we ...

Read More
Showing 2991–3000 of 4,498 articles
« Prev 1 298 299 300 301 302 450 Next »
Advertisements