Java Programming Examples

Java Programming Examples

Find the best practical and ready to use Java Programming Examples.

Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.

These examples would be very useful for your projects and learning.

Java Basics Examples

Example - Home Example - Environment Example - Strings Example - Arrays Example - Date & Time Example - Methods Example - Files Example - Directories Example - Exceptions Example - Data Structure Example - Collections Example - Networking Example - Threading Example - Applets Example - Simple GUI Example - JDBC Example - Regular Exp

Java Environment - Programming Examples

Learn how to play with Environment in Java programming. Here are most commonly used examples:

1. How to compile a java file? 2. How to run a class file? 3. How to debug a java file? 4. How to set classpath? 5. How to view current classpath? 6. How to set destination of the class file? 7. How to run a compiled class file? 8. How to check version of java running on your system? 9. How to set classpath when class files are in .jar file?

Saikat Banerjee

Page 1

Problem Description: How to compile a java file? Solution: Following example demonstrates how to compile a java file using javac command.

c:\jdk\demoapp> javac First.java

Result: The above code sample will produce the following result.

First.java compile successfully.

Problem Description: How to set multiple classpath? Solution: Following example demonstrates how to set multiple classpath. Multiple class paths are separated by a semicolon.

c:> java -classpath C:\java\MyClasse1;C:\java\MyClass2 utility.testapp.main

Result: The above code sample will produce the following result.

Class path set.

Problem Description: How to set destination of the class file? Solution: Following example demonstrates how to debug a java file using =g option with javac command.

c:> javac demo.java -g

Result: The above code sample will produce the following result.

Saikat Banerjee

Page 2

Demo.java will debug.

Problem Description: How to set classpath? Solution: Following example demonstrates how to set classpath.

C:> java -classpath C:\java\DemoClasses utility.demoapp.main

Result: The above code sample will produce the following result.

Class path set.

Problem Description: How to view current classpath? Solution: Following example demonstrates how to view current classpath using echo command.

C:> echo %CLASSPATH%

Result: The above code sample will produce the following result.

.;C:\Program Files\Java\jre1.6.0_03\lib\ext\QTJava.zip

Problem Description: How to set destination of the class file? Solution: Following example demonstrates how to set destination of the class file that will be created after compiling a java file using -d option with javac command.

c:> javac demo.java -d c:\myclasses

Result: The above code sample will produce the following result.

Saikat Banerjee

Page 3

Demo application executed.

Problem Description: How to run a class file? Solution: Following example demonstrates how to run a class file from command prompt using java command.

c:\jdk\demoapp>java First

Result: The above code sample will produce the following result.

Demo application executed.

Problem Description: How to check version of java running on your system? Solution: Following example demonstrates how to check version of java installed on your system using version argument with java command.

java -version

Result: The above code sample will produce the following result.

java version "1.6.0_13" Java(TM) SE Runtime Environment (build 1.6.0_13-b03) Java HotSpot(TM) Client VM (build 11.3-b02, mixed mode, sharing)

Problem Description: How to set classpath when class files are in .jar file? Solution: Following example demonstrates how to set class path when classes are stored in a .jar or .zip file.

c:> java -classpath C:\java\myclasses.jar utility.testapp.main

Saikat Banerjee

Page 4

Result: The above code sample will produce the following result.

Class path set.

Java String - Programming Examples

Learn how to play with strings in Java programming. Here are most commonly used examples:

1. How to compare strings? 2. How to search last occurance of a substring inside a substring? 3. How to remove a particular character from a string? 4. How to replace a substring inside a string by another one ? 5. How to reverse a String? 6. How to search a word inside a string? 7. How to split a string into a number of substrings ? 8. How to convert a string totally into upper case? 9. How to match regions in a string? 10. How to compare performance of two strings? 11. How to optimize string creation? 12. How to format strings? 13. How to concatenate two strings? 14. How to get unicode of strings? 15. How to buffer strings?

Problem Description:

How to compare two strings ?

Solution:

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 .

public class StringCompareEmp{ public static void main(String args[]){ String str = "Hello World"; String anotherString = "hello world"; Object objStr = str;

System.out.println( pareTo(anotherString) ); System.out.println( pareToIgnoreCase(anotherString) ); System.out.println( pareTo(objStr.toString())); }

}

Result:

The above code sample will produce the following result.

Saikat Banerjee

Page 5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download