Friday, 2 August 2013

compare Strings in Java

The example compares two strings by using str compareTo (string) , str compareToIgnoreCase(String) and str compareTo(object string) methods of string class and returns the ascii difference of first odd characters of compared strings .

Example of comparing Strings in Java

public class StringCompare{ 
public static void main(String args[]){ 
String str = "Nice World"; 
String anotherString = "nice world"; 
Object objStr = str; 

System.out.println( str.compareTo(anotherString) ); 
System.out.println( str.compareToIgnoreCase(anotherString) ); 
System.out.println( str.compareTo(objStr.toString())); 


}

Output

-32 

Java Strings

Java strings are objects designed to represent a sequence of characters. Because character strings are commonly used in programs, Java supports the ability to declare String constants and perform concatenation of Strings directly without requiring access to methods of the String class. This additional support provided for Java Strings allows programmers to use Strings in a similar manner as other common programming languages.

Creating a String

The simplest method to create a String object is to enclose the string literal in quotes and assign the value to a String object. Creation of a String through assignment does not require the use of the new operator, for example
String str = "abc";
String language = "Java";

Alternatively, String objects can be created through constructors. The following constructors are supported:

public String()

Constructs a new String with the value "" containing no characters. The value is not null.

public String( String value )

Constructs a new String that contains the same sequence of characters as the specified String argument.

public String( char[] value )

Constructs a new String containing the same sequence of characters contained in the character array argument.

Differences between String and StringBuffer in Java

Main difference between String and StringBuffer is String is immutable while StringBuffer is mutable means you can modify a StringBuffer object once you created it without creating any new object. This mutable property makes StringBuffer an ideal choice for dealing with Strings in Java. You can convert a StringBuffer into String by its toString() method.

members of Class

When we create a class its totally incomplete without defining any member of this class same like we can understand one family is incomplete if they have no members.
Field : Field is nothing but the property of the class or object which we are going to create .for example if we are creating a class called computer then they have property like model, mem_size, hd_size, os_type etc
Method : Method is nothing but the operation that an object can perform it define the behavior of object how an object can interact with outside world .startMethod (), shutdownMethod (). Access Level of members: Access level is nothing but where we can use that members of the class.
Each field and method has an access level: private: accessible only in this class package or default: accessible only in this package protected: accessible only in this package and in all subclasses of this class public: accessible everywhere this class is available The general form of a class definition is shown here:
Simple Java Example
  1. /*
  2. Java Hello World example.
  3. */
  4.  
  5. public class HelloWorldExample{
  6.  
  7.   public static void main(String args[]){
  8.  
  9.     /*
  10.     Use System.out.println() to print on console.
  11.     */
  12.     System.out.println("Hello World !");
  13.  
  14.   }
  15.  
  16. }
  17.  
  18. /*
  19.  
  20. OUTPUT of the above given Java Hello World Example would be :
  21.  
  22. Hello World !
  23.  
  24. */

What is Class?

A class is a blueprint or prototype that defines the variables and the methods (functions) common to all objects of a certain kind.
What does Java Class consist of
The first piece is a class-header which consists of the keyword "class" and the name you give to the class. Names in programming languages are also known as identifiers.
The second piece is the body. It consists of a pair of open/close curly brackets and between this all things related with that class means their property and method will come here.
Here is a template for the source code of a class:
class (Name of the class)
{
(Here define member of class)
...
}
Access level of class
Java class has mainly two type of access level:
Default: class objects are accessible only inside the package.
Public: class objects are accessible in code in any package.

What is Java?

Object Oriented : In java everything is an Object. Java can be easily extended since it is based on the Object model.
Platform independent : Unlike many other programming languages including C and C++ when Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by virtual Machine (JVM) on whichever platform it is being run.
Simple :Java is designed to be easy to learn. If you understand the basic concept of OOP java would be easy to master.
Secure : With Java's secure feature it enables to develop virus-free, tamper-free systems. Authentication techniques are based on public-key encryption.
Architectural - neutral :Java compiler generates an architecture-neutral object file format which makes the compiled code to be executable on many processors, with the presence Java runtime system.
Portable :being architectural neutral and having no implementation dependent aspects of the specification makes Java portable. Compiler and Java is written in ANSI C with a clean portability boundary which is a POSIX subset.
Robust :Java makes an effort to eliminate error prone situations by emphasizing mainly on compile time error checking and runtime checking.
Multi-threaded : With Java's multi-threaded feature it is possible to write programs that can do many tasks simultaneously. This design feature allows developers to construct smoothly running interactive applications.
Interpreted :Java byte code is translated on the fly to native machine instructions and is not stored anywhere. The development process is more rapid and analytical since the linking is an incremental and light weight process.
High Performance : With the use of Just-In-Time compilers Java enables high performance.
Distributed :Java is designed for the distributed environment of the internet.
Dynamic : Java is considered to be more dynamic than C or C++ since it is designed to adapt to an evolving environment. Java programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to objects on run-time.

A Simple Application for Java.


//This application program prints Welcome
//to Java!
public class Welcome { 
  public static void main(String[] args) {
    System.out.println("Welcome to Java!");
  }

}

OUTPUT

javac Welcome.java
java Welcome

Welcome to Java!

Characteristics of Java

01.Java is simple
02.Java is object-oriented
03.Java is distributed
04.Java is interpreted
05.Java is robust
06.Java is secure
07.Java is architecture-neutral
08.Java is portable
09.Java’s performance
10.Java is multithreaded

11.Java is dynamic