Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Java Articles
Page 117 of 450
What's the simplest way to print a Java array?
Generally, to print the contents of an array you need to use for loops, in addition to that you can directly print an array using the toString() method of this class. This method accepts an array as a parameter and returns it in String format.Exampleimport java.util.Arrays; public class PrintingArrays { public static void main(String args[]) { int[] myArray = {23, 93, 30, 56, 92, 39}; System.out.println(Arrays.toString(myArray)); } }Output[23, 93, 30, 56, 92, 39]
Read MoreWhat is the difference between integer and floating point literals in Java?
Integer literals represent fixed integer values like 900, 12, 400, -222 etc. (with in the integer range). Whereas, floating point literals represents fractional values i.e. numbers with decimal values like 25.53, 45.66, 58.66 etc. while writing these literals we should use the notation f or F as 25.53. Example public class StringExample { public static void main(String args[]){ int num1 = 100; float num2 = 30.0f; System.out.println("Value of integer:"+num1); System.out.println("Value of integer:"+num2); } } Output Value of integer:100 Value of integer:30.0
Read MoreWhat is the difference between character literals and string literals in Java?
Character literals represents alphabets (both cases), numbers (0 to 9), special characters (@, ?, & etc.) and escape sequences like , \b etc. Whereas, the String literal represents objects of String class. Example public class LiteralsExample { public static void main(String args[]){ char ch = 'H'; String str = "Hello"; System.out.println("Value of character: "+ch); System.out.println("Value of string: "+str); } } Output Value of character: H Value of string: Hello
Read MoreWhat are the default values of instance variables whether primitive or reference type in Java?
When we haven’t initialized the instance variables compiler initializes them with default values.For boolean type, the default value is false, for float and double types default values are 0.0 and for remaining primitive types default value is 0.Examplepublic class Sample { int varInt; float varFloat; boolean varBool; long varLong; byte varByte; short varShort; double varDouble; public static void main(String args[]){ Sample obj = new Sample(); System.out.println("Default int value ::"+obj.varInt); System.out.println("Default float value ::"+obj.varFloat); System.out.println("Default boolean value ::"+obj.varBool); ...
Read MoreCan static variables be called using className.variableName in Java?
Class variables also known as static variables are declared with the static keyword There would only be one copy of each class variable per class, regardless of how many objects are created from it. You can access a class variable without instantiation using the class name as className.variableName. Example public class Test{ static int num = 92; public static void main(String args[]) throws Exception { System.out.println(Test.num); } } Output 92
Read MoreWhat are final, abstract, synchronized non-access modifiers in Java?
The abstract keyword is used to declare methods abstract methods and abstract classes. Once a method is declared abstract we should not specify body for those. And once a class is declared abstract it cannot be instantiated. Example abstract class Employee { private String name; private String address; private int number; public Employee(String name, String address, int number) { System.out.println("Constructing an Employee"); this.name = name; ...
Read MoreWhat does synchronized modifier do in Java?
The synchronized keyword used to indicate that a method can be accessed by only one thread at a time. The synchronized modifier can be applied with any of the four access level modifiers. Example public class TestThread { public static Object Lock1 = new Object(); public static Object Lock2 = new Object(); public static void main(String args[]) { ThreadDemo1 T1 = new ThreadDemo1(); ThreadDemo2 T2 = new ThreadDemo2(); ...
Read MoreWhat are unchecked exceptions in Java?
An unchecked exception is the one which occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation. If you have declared an array of size 5 in your program, and trying to call the 6th element of the array then an ArrayIndexOutOfBoundsExceptionexception occurs.Examplepublic class Unchecked_Demo { public static void main(String args[]) { int num[] = {1, 2, 3, 4}; System.out.println(num[5]); } }OutputException in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at Exceptions.Unchecked_Demo.main(Unchecked_Demo.java:8)
Read MoreCan we define an interface inside a Java class?
Yes, you can define an interface inside a class and it is known as a nested interface. You can’t access a nested interface directly; you need to access (implement) the nested interface using the inner class or by using the name of the class holding this nested interface.Examplepublic class Sample { interface myInterface { void demo(); } class Inner implements myInterface { public void demo() { System.out.println("Welcome to Tutorialspoint"); } } public static void main(String args[]) { Inner obj = ...
Read MoreDoes Java support default parameter values for a method?
Java does not support the concept of default parameter however, you can achieve this usingMethod overloadingUsing method overloading if you define method with no arguments along with parametrized methods. Then you can call a method with zero arguments.Variable argumentsIn Java methods parameters accept arguments with three dots. These are known as variable arguments. Once you use variable arguments as a parameter method, while calling you can pass as many number of arguments to this method (variable number of arguments) or, you can simply call this method without passing any arguments.Examplepublic class Sample { void demoMethod(String... args) { ...
Read More