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 112 of 450
How to initialize and compare strings?
Following example compares two strings by using str compareTo (string), str compareToIgnoreCase(String) and str compareTo(object string) of string class and returns the ascii difference of first odd characters of compared strings.Examplepublic class StringCompareEmp{ public static void main(String args[]) { String str = "Hello World"; String anotherString = "hello world"; Object objStr = str; System.out.println( str.compareTo(anotherString) ); System.out.println( str.compareToIgnoreCase(anotherString) ); System.out.println( str.compareTo(objStr.toString())); } }OutputThe above code sample will produce the following result.-32 0 0String compare by equals()This method compares this ...
Read MoreHow to initialize and compare strings?
Following example compares two strings by using str compareTo (string), str compareToIgnoreCase(String) and str compareTo(object string) of string class and returns the ascii difference of first odd characters of compared strings.Examplepublic class StringCompareEmp{ public static void main(String args[]) { String str = "Hello World"; String anotherString = "hello world"; Object objStr = str; System.out.println( str.compareTo(anotherString) ); System.out.println( str.compareToIgnoreCase(anotherString) ); System.out.println( str.compareTo(objStr.toString())); } }OutputThe above code sample will produce the following result.-32 0 0String compare by equals()This method compares this ...
Read MoreCan we overload the main method in Java?
Yes, we can overload the main method in Java, but When we execute the class JVM starts execution with public static void main(String[] args) method. Example public class Sample{ public static void main(){ System.out.println("This is the overloaded main method"); } public static void main(String args[]){ Sample obj = new Sample(); obj.main(); } } Output This is the overloaded main method
Read MoreSingle dimensional array in Java
Following is a simple example of a single dimensional array.Examplepublic class Tester { public static void main(String[] args) { double[] myList = {1.9, 2.9, 3.4, 3.5}; // Print all the array elements for (double element: myList) { System.out.print(element + " "); } } }Output1.9 2.9 3.4 3.5
Read MoreHow to determine the OS the computer is running using Java?
The System class of java.lang package provides a method named getProperty() this method accepts one of the following string parameters an returns the respective property. java.class.path − If you pass this value as a parameter, the getProperty() method returns the current classpath. java.home − If you pass this value as a parameter, the getProperty() method returns the current Installation directory of the JRE. java.vendor − If you pass this value as a parameter, the getProperty() method returns the current vendor name of the JRE. java.vendor.url − If you pass this value as a parameter, the getProperty() method returns the ...
Read MoreWhat are method local inner classes in Java?
In Java, we can write a class within a method and this will be a local type. Like local variables, the scope of the inner class is restricted to the method.A method-local inner class can be instantiated only within the method where the inner class is defined. The following program shows how to use a method-local inner class.Examplepublic class OuterClass { public void display(){ int num = 23; class Inner{ public void print() { System.out.println("This is method inner class "+num); ...
Read MoreCan you extend a static inner class in Java?
A static inner class is a nested class which is a static member of the outer class. It can be accessed without instantiating the outer class, using other static members. Just like static members, a static nested class does not have access to the instance variables and methods of the outer class. You can extend static inner class with another inner class. Example public class SampleClass { static abstract class Test{ int num = 300; public abstract void display(); } ...
Read MoreAre static methods inherited in Java?
The static keyword is used to create methods that will exist independently of any instances created for the class. Static methods do not use any instance variables of any object of the class they are defined in. Static methods take all the data from parameters and compute something from those parameters, with no reference to variables. We can inherit static methods in Java. Example In the example we are creating a class named Demo and, declared a static method named display(). We created another class Sample, extended the Demo class and tried to access the display() method using the sub ...
Read MoreIs there a need to import Java.lang package while running Java programs?
The java.lang package is the default package in Java, by default, it will be imported. Therefore, there is no need to import this package explicitly. i.e. without importing you can access the classes of this package. Example If you observe the following example here we haven’t imported the lang package explicitly but, still, we are able to calculate the square root of a number using the sqrt() method of the java.lang.Math class. public class LangTest { public static void main(String args[]) { int num = 100; ...
Read MoreHow to write single line comment in Java?
To comment a particular line just place ‘double back slash (//)’ before the line as shown below. // Hello this line is commented Example Following example demonstrates the usage of single line comments in Java. public class CommentsExample { public static void main(String args[]) { //Declaring a variable named num int num = 1; //Printing the value of the variable num System.out.println("value if the variable num: "+num); } } Output value if the variable num: 1
Read More