Wednesday, June 27, 2012

Simple Way to learn Java Programing in two Days
JAVA PROGRAMING SIMPLE STEPS TO DEVELOP APLLICATIONS ND CODING

What is a CLASS & OBJECT?

How do you differentiate these two ? Explain with examples.

A class is a template, blueprint, plan,design or protype from wichObjects can be derived If you create an object from a class , it means you created an instance of a class.

A SMALL EXAMPLE IN MY VIEW.....

Suppose if you are planning to construct a house first step what you will do ? you will go for design or plan of it, Once plan is ready you will start actual construction. By keeping this plan with you, construct one more house ,like wise you can construct any number of houses. Now here this plan is a class ,and actual construction is object. this class doesn't occupy any memory only object occupies memory. From one class we can create ‘N’ number of objects.

Few points on Structured programming and Object oriented programming?

DESIGNER OF PASCAL LANGUAGE NIKLAUS WIRTH ONCE SAID

"ALGORITHMS+DATASTRUCTURES=PROGRAMS".

HERE ALGORITHM COMES FIRST AND DATA STRUCTURES COMES SECOND.THIS IS HOW PROGRAMMERS WERE THINKING AT THAT TIME .FIRST THEY DECIDED THE PROCEDURES FOR MANIPULATIONS OF DATA, THEN THEY DECIDED WHAT STRUCTURE TO IMPOSE ON DATA TO MAKE MANIPULATIONS EASIER.OOP REVERSES THE ORDER AND PUTS DATA FIRST, THEN LOOKS AT THE ALGORITHM THAT OPERATE ON THE DATA. FOR SMALL PROGRAMS PROCEDURES WORKS WELL, BUT OBJECTS ARE BETTER FOR LARGER PROGRAMS. DEBUGGING ALSO EASIER WITH OOP.

Goals of JAVA!

When Java was invented it has few goals in mind,

1.It should be simple

2.It should be Object-Oriented

3.It should be “robust and secure”

4.It should be “platform independent and portable”

5.It should execute with high performance

6.It should be “interpreted, threaded and Dynamic”

What is JAVA ?

JAVA IS A PROGRAMMING LANGUAGE.

AS A WHOLE JAVA IS A PLATFORM, WITH LARGE LIBRARY, CONTAINING LOTS OF REUSABLE CODE, AND AN EXECUTION ENVIRONMENT THAT PROVIDE SERVICES SUCH AS SECURITY, PORTABILITY ACROSS SYSTEMS AND AUTOMATIC GARBAGE COLLECTION.

JAVA IS A HIGH QUALITY ENVIRONMENT AND LIBRARY.

History of JAVA

Actually Java was originally designed for programming consumer devices, but it was first succeful ly used to with internet applets.In the year 1991, when a group of SUN engineers (which is now subsidiary of oracle corporation) led by James Gosling, Patrick Nauton, Mile Sheridon wanted to design a small computer language that could be used for consumer devices like, cable TV switch boxes, remote controller, Microwave oven. Because these devices do not have lot of memory the language had to be small and generate very tight code. Also, because different manufacturers may choose different CPUs, it was important that the language not be tied to as single architecture, the project was code-named “Green”.

Later it renamed as “OAK” after an oak tree that stood outside gosling’s office. Then it renamed as JAVA.

What is Programming?

NOW A DAY COMPUTERS BECAME PART OF HUMAN LIFE .MANY PEOPLE USES IT FOR DIFFERENT PURPOSES LIKE PLAYING GAMES, BANKING TRANSACTIONS, AND FOR ONLINE PURCHASES. THESE COMPUTERS HAS NO INTELLIGENCE BY ITSELF, IT SIMPLY EXECUTES INSTRUCTIONS THAT HAVE BEEN PREPARED IN ADVANCE. IN ORDER TO ACHIEVE THIS FLEXIBILITY THE COMPUTERS MUST BE PROGRAMMED TO PERFORM EACH TASK, DIFFERENT TASKS REQUIRE DIFFERENT PROGRAMS. THESE PROGRAMS ARE SEQUENCE OF INSTRUCTIONS AND DECISION THAT COMPUTERS CARRYOUT TO ACHIEVE A TASK. THE ART OF WRITING THESE KINDS OF PROGRAMS IS NOTHING BUT PROGRAMMING.

Simple Way to learn Java Programing in two Days

STRINGS BASIC PROGRAM SERIES-1

PROGRAM TO WRITE A REVERSE OF A STRING:-

package com.javainchennai;

import java.util.Scanner;

public class StringReverse {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter a String");

String str=sc.nextLine();

int strLen=str.length();

char c[]=new char[20];

for(int i=0,j=strLen-1;i<strLen;i++,j--)

{

c[i]=str.charAt(j);

System.out.print(c[i]);

}

}

}

OUTPUT:-

Enter a String

madam123

321madam

STRING PALINDROME PROGRAM

package com.javainchennai;

import java.util.Scanner;

public class Stringpolindrome {

public static void main(String[] args) {

int i,j=0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter your String:-");

String str=sc.next();

char c[]=new char[20];

c=str.toCharArray();

i=str.length();

--i;

while(j<=i)

{

if(c[i]!=c[j])

{

System.out.println("The given string "+ str +" is not polindrome");

System.exit(0);

}

++j;

--i;

}

System.out.println("The given string" + str +"is polindrome");

}

}

OUTPUT-1 :

Enter your String:-

javainchennai

The given string javainchennai is not polindrome

OUTPUT-2 :

Enter your String:-

malayalam

The given string malayalam is polindrome

FINDING THE LENGTH OF A STRING

package com.javainchennai;

import java.util.Scanner;

public class StringLength {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("Please Enter your String:-");

String str=sc.nextLine();

char c[]=new char[10];

c= str.toCharArray();

int count=0;

try{

for( count=0;c[count] !='\0'; count++);

}

catch (ArrayIndexOutOfBoundsException e) {

System.out.println(count);

}

}

}

Please Enter your String:-

questionabanks

The Length of the Given String is: 13

FINDING THE NUMBER OF WORDS IN A STRING

package com.javainchennai;

import java.util.Scanner;

public class NoOfWordsInAString {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter your string:-");

String str=sc.nextLine();

int strLen=str.length();

int word=1;

for(int i=0;i<strLen-1;i++)

{

if((str.charAt(i))==' ')

{

word++;

}

}

System.out.print("The Number of words in the Given String are :" +word);

Before going into programs a small introduction on Strings

If there is any mistakes in this article please write me blog4helping@gmail.com

======================

-:STRINGS:-

======================

1.String is a class in Java

2.String means collection of characters

3.String is a sequence of characters enclosed in double Quotes.

4.Java Strings are sequence of Unicode characters

5.String class produces Immutable objects.

6.String class does not have a methods that let you change a characters in an existing String.

7.It is like number 5 , the String "HELLO" we cannot change it.

Sting Objects can be created in 2 ways

1. without using new operator

String str1="javainchennai";

2. with using new operator

String str2=new String("javainchennai");

The memory space is divided in to 2 parts

1.constant area

2.non-constant area

If you create object without using new operator this will be saved in constant area

String str1="javainchennai";

If you create object with using new operator this will be saved in non-constant area

String str2=new String("javainchennai");

Here Constant area will not allow duplicates

where as non-constant area will allow duplicates.

Do Remember inside a String class the toString() method is overided

Here if you try to print reference variable

String s1="javainchennai";

System.out.println(s1);

The output is javainchennai instead of String@ff2201(classname @ hexadecimal value)

equals() method also overrided to check 2 objects based on the value(content) it contains

observe the program carefully and read the explanation:-

1.package com.qb;

2.public class StringTest {

3. public static void main(String[] args) {

4. String str1="javainchennai";

5. String str2="javainchennai";

6. String str3=new String("javainchennai");

7. String str4=new String("javainchennai");

8. System.out.println(str1==str2);

9. System.out.println(str1==str3);

10. System.out.println(str2==str3);

11. System.out.println(str3==str4);

12. System.out.println(str1.equals(str2));

13. System.out.println(str1.equals(str3));

14. System.out.println(str2.equals(str3));

15. System.out.println(str3.equals(str4));

16. }

17. }

output:-

true

false

false

false

true

true

true

true

explanation:-

If you say 5==5 is true, here two are primitive literals so == operator(checks content) prints true.

If you use == operator to compare two object references it will see whether both references are pointing to same object or different.

If both references are pointing to same object it(== operator) returns true otherwise false.

observe line4 and line5 ,here actually I created String object without using new operator so, str1 and str2 both references are pointing to same object (javainchennai). (constant area will not allow duplicates)

so, str1==str2 returns true.

observe line4 and line5 ,here actually I created String object by using new operator so, str3 and str4 both references are pointing to different objects (javainchennai).(non constant area allows duplicates).

So, str3==str4 returns false.

Observe the same effect with equals() method it returns true all time .(because it is overrided to compare based on content inside object)

PROGRAMS:-

program:-

1.public class StringTest {

2. public static void main(String[] args) {

3. System.out.println(3+5);

4. System.out.println("3"+5);

5. System.out.println(3+"555");

6. System.out.println(3+2+5+"4444");

7. System.out.println("35"+111+"99");

8. System.out.println("42"+11+32+"77");

9. System.out.println("100"+"222");

}

}

OUTPUT:-

8

35

3555

104444

3511199

42113277

100222

Explanation:-

-------------

In Java there is no operator overloading concept.

+ is the only one operator overloaded to add 2 integers as well as 2 strings

In the above example we added 2 primitives that is common ,

when we added String with any primitives only String object is produced.

observe the output of lines 6,7,8,9.

DIFFERENCES BETWEEN STRING , STRINGBUFFER AND STRINGBUILDER

String:-

------------------

1.String class introduced in jdk1.0

2.String class is a final class and implements Serializable,Comparable and CharSequence interfaces

3.String Objects can be created in two ways
a.Using new operator

constructors to create String ob

b.Without using new operator
4.-----
5. String class has several
jects(both using new or without using new operator)
a.String s1="";