Saturday, December 11, 2010

when object is created when refrence is assigned???

*Remeber object is always created when you declare an object of class type and refrence is always assigned when u called constructor of that class.
for example

class welcome{
private String greet;
public welcome()
{
greet="Welcome to Java......................!!!!";
}
public void displayGreeting()
{
System.out.println("Greeting :   "+greet);
}
}
public class Demo
{
public static void Main(String[] args)
{
1) welcome w;          
2) w.displayGreeting();           //generates an error of uninitialized object but the matter it has no refrence and                  dont know where to point.
3) welcome wel=new welcome();         //object type :welcome and refrence of:welcome
4) wel.displayGreeting();
}
}
*Remember statment 1 and 2 will not compile and elt the program run but statment 3 and 4 will behave exactly as like an normal and successful program is supposed to behave.

how to call methods of a class???

Lets make again the welcome programme with different strategy so that we  could see how to call a method belonging to a class vis its object.

class welcome{
private String greet;
public welcome()
{
greet="Welcome to Java......................!!!!";
}
public void displayGreeting()
{
System.out.println("Greeting :   "+greet);
}
}
public class Demo
{
public static void Main(String[] args)
{
welcome w=new welcome(); /////now constructor will only initialize and will do nothing else
w.displayGreeting();
}
}
*This programe behave Exactly same as last one but biggest difference is that here constructor only initializes and does nothing else until 
w.displayGreeing();
line called no greetings are displayed on screen.

how can i create a class??

Usually when you create a project a class is generated for you automatically by java IDE ( i m using netbeans 6.9.1). But thats not all about you can make almost as many classes as u want:
for a generic class m making a class with its constructor displaying welcome message
if you want to add new class in same folder as the main class dont use identifier as public(m doing so)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class welcome{
public welcome()
{
System.out.println("Welcome to Java......................!!!!");
}
}
public class Demo
{
public static void Main(String[] args)
{
welcome w=new welcome();
}
}
*Remember classes are always like a container which contains some things and any thing belonging to class are called via its object.
*Remember
public welcome(){}
its not a method in fact it is constructor. Code in consructor execute before all when we create an object of a class thats y all initialization code are inside of constructor.

what are the classes and whats its importance in java??

Classes are the basic building block of java. In java every thing is based on classes. Not only java classes are very important in almost every object oriented language in the programming world.Classes provide encapsulation to the programms and make the application to the user like a black box( whose internal working is not concerned just what it does is the real matter to concerned about).

What does the break statment do???

break statment is usually used to stop the loop execution immediately with our letting it to complete its total iterations. It is usually used for infinite loops to search for a specific condition.
for example
public class Demo
{
public static void Main(String[] args)
{
int i=0;
while(1){
i++;
if(i= =10)
{
break;                    //will jump outside the loop when i equals 10
}
}
}
}

Sunday, December 5, 2010

General comments

Hey Guys !! If feel some difficulty or wana some knowledge about other topic in java but write here in your comments or inbox me or leave your email id . i will help you be sending you the programme over there.

General comments

Hey Guys !! If feel some difficulty or wana some knowledge about other topic in java but write here in your comments or inbox me or leave your email id . i will help you be sending you the programme over there.

Worry about Switch Statements in Java .................!!

No you don't need to worry i m here to help you out for Java ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,



class Switch {
public static void main(String args[]) {
for(int i=0; i<6; i++)
switch(i) {
case 0:
System.out.println("i is zero.");
break;
case 1:
System.out.println("i is one.");
break;
case 2:
System.out.println("i is two.");
break;
case 3:
System.out.println("i is three.");
break;
default:
System.out.println("i is greater than 3.");
}
}
}

Can i have Nested If statements in Java................................!!

Yeah why not you can have with no need of knowing rocket engineering.

public static void main(String[] args)
{
int a,b,c                //to prove symetic property of mathem(a>b and b>c mean a>c)
if(a>b)
{
if(b>c)
{
System.out.println("Its Symmetric propertry of mathematics proves a>c");
}
else
{
System.out.println("Its not Symmetric property of mathemtics");
}
}
}
You can make above programme with any other side logic but i just wana show an example of nested if
statements and this pop up to my mind.

How to use if statements in Java??

"If statements" could be used in java in the way as:
int b,a;;
if(b= = a)
{
//do here some thing
}
else
if(b>a)
{
a=0;
}
else
{
b=0;
}
Above program is in main function it will operate as two integer will be checked first of all(assign both of them any value i did'nt else program will not run) if both are equal do nothing(you can customize if body and force it to do anything). Then it will check int b whether its larger than int a or not if yes then it will assign int a=0 in the if body. If both of first two conditions failed then it will enter in else statment and assign int b=0(knowing that if int b is not larger or neither equal to it then int a must be larger than int b)

What are the precedence(importance) of operator

Precedence or importance mean which operator will execute first if different operator come in one statment at a time:

Highest
( )                                 [ ]                           .
++                               – –                          ~                                !
*                                   /                               %
+                                  –
>>                              >>>                            <<
>                                >=                              <                               <=
= =                              !=
&
^
|
&&
||
?:
=                                op=
Lowest
Above is precedence chart form highest to lowest(from top to bottom)................................................

what are the basic mathematical operator for mathematical work in java

There are some mathematical operator for providing basic mathematical functions like......

Operator                                                              Result
+                                                                       Addition
–                                                                       Subtraction (also unary minus)
*                                                                       Multiplication
/                                                                        Division
%                                                                     Modulus
++                                                                    Increment
+=                                                                    Addition assignment
–=                                                                    Subtraction assignment
*=                                                                    Multiplication assignment
/=                                                                     Division assignment
%=                                                                   Modulus assignment
– –                                                                    Decrement
You can perform some basic mathematical functionality with these operator but can increase your skill by using Math class in java in way like
int b=Math.somefunction(somevalue);  

I Search for constant keyword in java but can,t find why???????????

Thats because Java dont have any keyword constant or const keyword to declare constant variables but rahter them java has final keyword to create constant variables.......................................................!!

how can i make functions in java

In java functions are exactly same as in other langauges.
A function comprises the following things
public void functionname(int a)
{
//do any thing with int a
}
1) A function has a access modifier(public,private,protected) which can be used according to requirement.
2) return time of a function which can be any one in data types like could b strings,int, double,array any thing
3) function name which could be any thing but dont start it with numbers and special characters.
4) parameters of functions which could any number of parameters like functionname(int a,double b,int c){}
5) body of function perform any process here like add parameters multiply with constants and any thing you can do here.
*Remeber when some one ask what method signature always reply its function name and parameter list only.

Thursday, December 2, 2010

do while loop in java

do while loop is all languages including java has one drawback that this loop will execute for once under all circumstances even the condition satisfies or not.
it begins with do keyword followed by body of loop and in the end condition.

int i,count=0;
do
{
i++;
count++;
}while(i<9);
System.out.println("Count :   "+count);

While loop is java

While loop has only condition to be started

count=0;
int i=0;
while(i<9)                                     //just condition checking
{
count++;
i++;                                            //dont forget this counter else loop going move ever and ever(infinite loop)
}
System.out.println("Count :   "+count);

for Loop in java

Like other languages java has 3 basic loops and one advance loop.
For Loop:
i) first part is initial value assignment.
ii) 2nd part is condition to be checked.
iii) 3rd part is increment or decrement
int count=0;
for(int i=0;i<10;i++)
{
count++;

}
System.out.println("Count :  "+count);

how to create arrays in java

public class demo
{
public static void main(String[] args)
{
int[] arr=new int[10];                    //create array of 10(0-9) index to store integers
int arr[]=new int[10];                    //create array of 10(0-9) index to store integers

}
}
Both of statments in main function are same in java we can use both of notations but i my self would prefer the first one to be used.

How can i take Input a variable from console

import java.util.*;    
public class Demo
{
static private Scanner input =new Scanner(System.in);                  //static becz we are going to use it in static                   method
public static void main(String[] args)
{
double data;
System.out.print("Please Enter Data :  ");                        //displaying msg to inpuut data
data=input.nextDouble();                                                // assigning double to data variable
System.out.println("Data :    "+data);

}
}
                                      

what are shadow variables in java

A shadowed variable is a variable that would otherwise be accessible, but is
temporarily made unavailable because a variable with the same name has
been declared in a more immediate scope.
Example:

public class ShadowApp
{
static int x;
public static void main(String[] args)
{
x = 5;
System.out.println(“x = “ + x);
int x;
x = 10;
System.out.println(“x = “ + x);
System.out.println(“ShadowApp.x = “ +
ShadowApp.x);
}
}

whats parsing and how can i do this

Parsing is a technique with which we can convert string to our desired primitive data type via using wrapper classes.
Example:
String data="10";                                //10 is a string right now
int a=Integer.parse(data);                   //now 10 is converted to integer
  same procedure can be applied for other primitive data types

what are the wrapper class and what is their purpose in java???

Wrapper classes: Every primitive type has a corresponding class defined in the Java API class
library. This class is sometimes called a wrapper class. These classes are mostly used for parsing of some string into primitive data type.(primitive data type=int,float,char,double,long,byte,short,boolean)
listed below are data type and their wrapper classes.
Primitive Type                           Wrapper Class
int                                                 Integer
short                                             Short
long                                              Long
byte                                              Byte
float                                             Float
double                                         Double
char                                           Character
Boolean                                     Boolean

Wednesday, December 1, 2010

Examples of class variable

public class Demo
{
String data;            
static String msg;
public void main(String[] args)
{
data="Java is superb...................!!";  //this is error line because in static methods on static variables can be accessed.

msg="Java is superb...................!!";          //this statment will work fine
}
}

how to create class variable

class variables are those variables which are accessible at class level. There are two rules for them
1) those variables must be within the class boundary.
2)must place a word static before variables. The static word comes before data type(int,float) of variables.

Tuesday, November 30, 2010

how can i create variable in java !!!

well creating variables in java is extremely easy like all other programming languages....................................................
integers can be created as

int data;
int data=5;
int data,result,sum;

long can be created as

long data;
long data=14.0;
long data,result,sum;

float can be 
float data; 
float data=4F;                  //this statment will execute as well and will convert 4 to float 4. this is called literals.
float data,result,sum;



double can be

double data;
double data=10D;        //this statment will execute as well and will convert 4 to double 4. this is called literals.
double data,result,sum;
characters can be created as
char data;
char data='k';
String could be created as
String data;
String data="hi ! this is java programme";

Statments type

Java has many different types of statements. Some statements simply create
variables that you can use to store data. These types of statements are often
called declaration statements, and tend to look like this:
int i;
String s = “This is a string”;
Customer c = new Customer();

Another common type of statement is an expression statement, which performs
calculations. Here are some examples of expression statements:
i = a + b;
salesTax = invoiceTotal * taxRate;
System.out.println(“Hello, World!”);

why main class is public and main is static public

Usually word public means that class is accessible for all. Using public keyword with main mehod containing class means that class cobsisting main method should be accessible for other classes.
Static Keyword means that method is accessible at class level without need to create object first as no object could be created before main method called (main is entry point of any programme) so thats why main method is created at class level not object level.

Monday, November 29, 2010

Welcome Programme in c#

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace SimpleCSharpApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to c#............................!!!!");
}
}
}

Learn Csharp

Csharp is a morcosoft language to compete java thats why both of them are called sister languages. For C# you just first install visual c# 2009. c# is also a fully object oriented language.

Learn Java

To learn Java you must first install and java IDE which you can easily get from internet. I would prefer either net beans or either eclipse. Remember java is fully object oriented language and thus it forces you to write any piece of code in classes. For java first download JDK 6 which is essential for java application. All classes in java are inherited from object class so there is no need of importing that library.

Java welcome programe

public class demo{
public static void main(String[] args)
{
System.out.println("Welcome to JAVA........................!");
}
}