Saturday, June 19, 2010
Friday, June 18, 2010
Data Types
List of primitive data types:-
charint (long, short, byte)
float (double)
boolean
Java also has the String data type that accepts a sequence of characters in a go, with a large containing capacity.
Keywords
Java has several of them, here are some:-
transient ||
for ||
do ||
while ||
if ||
else ||
synchronized ||
new ||
class ||
void ||
int ||
char ||
byte
long ||
short ||
boolean ||
public ||
protected ||
private ||
switch ||
case ||
default ||
enum ||
extends ||
implements ||
final ||
try ||
catch ||
finally ||
float ||
double ||
package ||
return ||
static
super ||
strictfp ||
const ||
break ||
assert ||
abstract ||
continue ||
throw ||
throws ||
this ||
volatile ||
goto ||
import ||
interface ||
native ||
EXERCISE: Count the number of keywords in Java...
(Answers as comments)
Now, let's start!!
Identifiers
An identifier is what it literally means. It is like a unique identity for a class, variable, object, or a function, or anything needing an identifier. Some rules need to be followed while declaring an identifier:-
1) For a variable:-
Can have alphanumeric characters ((A-Z), (a-z), (0-9), ($, _))
It can start with $ or _, but it cannot start with a number.
No spaces allowed.
Below are some naming conventions that are recommended(not necessary to be followed).
IDEAL CONVENTIONS FOR NAMING VARIABLES
Keep the variable name as something sensible, not any random sequence. Preferably, the name should be related to the context the variable is being used in. For example, the variable storing the sum of two numbers can be called as sum. Variables like i, or j, or i, or i2 are recommended for throwaway variables like those used in loop constructs. The first letter should always be in lower case, and as no spaces are allowed, the subsequent words can be written using the Camel Case. For example, a variable for the sum of two integers can be as sumOfIntegers, the first letter of each new word being capital. Where we need to use two words, we can also use an _(underscore) as a separator. For example, money_obtained, credit_left, instead of moneyObtained or creditLeft, of which both are correct, too.
2) For a function:-
Can have alphanumeric characters ((A-Z), (a-z), (0-9), (_)).
Can't begin with a number.
No spaces allowed.
Below are some naming conventions that are recommended(not necessary to be followed).
IDEAL CONVENTIONS FOR NAMING FUNCTIONS
The naming conventions are the same for functions as they are for variables, first letter of first word in lowercase, and subsequent first letters of words in higher case, or underscores, which are less recommended by us. Name should ideally be a verb, or a verb-noun combination.
1) For a class:-
Can have alphanumeric characters ((A-Z), (a-z), (0-9), (_)).
Can't start with a number.
No spaces allowed.
Below are some naming conventions that are recommended(not necessary to be followed).
IDEAL CONVENTIONS FOR NAMING CLASSES
Same as functions, except the first letter should always be a capital letter, and the name should ideally be a noun.
Step1: Getting the Java Development Kit(JDK)
The latest version for JDK as per our knowledge is JDK 1.6 update 20. It is available at http://java.sun.com. From there, go to downloads, and then JDK. You will receive the Java Runtime Environment along with it. For windows users only:
Setting the path:-
Go to My Computer's properties by right clicking on Start -> Explore all users
On the left, you will see "My Computer". Right-Click on it, and go to Properties.
Go to the "Advanced" tab, and click on Environment Variables. Navigate to the path variable,
and change it to:-
JDK_root/bin where JDK_root signifies wherever your JDK has been installed.
Then, create a new variable, and type the name as "classpath".
Fill in it's value as JDK_root/lib. You know what JDK_root signifies, we expect.
That's it, your JDK paths have been set!!
With this, you can create programs in Notepad, and save them as .JAVA, and then compile them as follows:-
javac filename.java
Run them as follows:-
java filename
(.java not required over here)
All in the command-prompt, again on Windows only.
(For other users, on other Operating Systems, and for people who don't want to work with Notepad, download NetBeans IDE for your platform, or Eclipse IDE, or NaviCoder IDE for Java.
All three are equally good. )
Coming up...
NOW: Identifiers, Keywords, and Types
Introduction to OOPS
Object-Oriented Programming System is a method of programming, in which programs are coded using well-built objects and inter-relating them. There are many advantages of the OOP System:-
- Modularity of the program:- The programs created using object-oriented programming are modular, that means that they have different modules for different tasks in the form of functions.
- Re-usability of code:- The code is always reusable, as the same modules can be called from time to time instead of writing them over and over again.
- Advantages over procedural programming:- In procedural programming the code is never reusable, and the programming is not modular.
About the Java Programming Language
The Java programming language has been developed by Sun Microsystems, and was
primarily known as Oak. Before, Java was intended for web applets only, but now it
has expanded into a full-fledged Object-Oriented language, and a replacement for C++,
as it does not have most of the flaws that c++ has, like manual garbage memory
De-allocation. Thus, here we start our journey with Java(Written with C++).
Coming up now...
Identifiers, Keywords, and Types
Thursday, June 17, 2010
Java - "Hello World"
Before you start learning any language, even before it's theoretical part, the Hello World example always gives you an idea about that language. About that today:-
Hello World in JAVA
public class Main
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
In-Depth: The System.out.println("Your - Message") function, note that it is a function, prints the "Your - Message" string to the console.
TOMORROW: Java: Get the JDK and an IDE, and an Introduction to Object-Oriented Programming...
Subscribe to:
Comments (Atom)