Java Articles

Page 112 of 450

How to initialize and compare strings?

Vikyath Ram
Vikyath Ram
Updated on 11-Mar-2026 230 Views

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 More

How to initialize and compare strings?

Vikyath Ram
Vikyath Ram
Updated on 11-Mar-2026 230 Views

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 More

Can we overload the main method in Java?

radhakrishna
radhakrishna
Updated on 11-Mar-2026 3K+ Views

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 More

Single dimensional array in Java

V Jyothi
V Jyothi
Updated on 11-Mar-2026 1K+ Views

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 More

How to determine the OS the computer is running using Java?

Sreemaha
Sreemaha
Updated on 11-Mar-2026 241 Views

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 More

What are method local inner classes in Java?

Prabhas
Prabhas
Updated on 11-Mar-2026 2K+ Views

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 More

Can you extend a static inner class in Java?

Giri Raju
Giri Raju
Updated on 11-Mar-2026 2K+ Views

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 More

Are static methods inherited in Java?

mkotla
mkotla
Updated on 11-Mar-2026 8K+ Views

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 More

Is there a need to import Java.lang package while running Java programs?

Sreemaha
Sreemaha
Updated on 11-Mar-2026 755 Views

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 More

How to write single line comment in Java?

mkotla
mkotla
Updated on 11-Mar-2026 616 Views

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
Showing 1111–1120 of 4,498 articles
« Prev 1 110 111 112 113 114 450 Next »
Advertisements