Java lesson 1.0.1.5

ravicplk

Member
Oct 18, 2007
4,120
1
0
39
Sri Lanka
Hello there my friends! How is everything going? I hope that everything is going fine. First little reminder to all of you, be careful when u are traveling in a public transport service. And don’t stay in busy areas for nothing. Coz bombs are every where. And sorry for the delay.

Ok now lets move on to our lesson. In the last lesson we learnt about data types. Think u remember that
There are 8 main data types and they are byte, short, int, long, double, float, char, Boolean,
I think u can remember those things.

Ok now lets talk about today’s lesson-1 .


LITERALS

1)
So as u know there are sooo many values included in a single program. Not only in Java, in any program there are values included in them. Hey, what came to u r mind when I said values. I know u only think about 1,2,3… nee. Why the hell is that, we think only about numbers ,when we heard the word “value” . Must be a mind thing. Anyway when we say values we can include many things to it. I don’t know in other subjects, but in programming the word value is used to so many things.

2)
So can u guess the values in programming? As we discussed earlier we have numbers, then we have character values, boolean values such as true and false. And finally we have string values. Let’s talk about these things in detail in a minute. So all these values represent themselves in a program. Hmmm….. you might be saying”what the hell is that he said” . Forget it guys. Let me explain it with a example. Ok. Lets see…ahaaaaa got one. So in EK there are sooo many members. Lets think of one. Hmm.. “Xpert ayya” ,
Why the hell he came to my mind..ahaa..coz he is the one inspired me to do these threads.

So think about xpert ayya. He is the only xpert in EK. Xpert is a constant thing(rather a person) in EK. And always he represent him self, that mean that he is not a puppet like thing. So if we compare that example with a program, EK is the program and Xpert is the literal. Hmm not the best example. If u don’t understand the concept please post.ok.


(guys who don’t know about Xpert ayya may think who the hell he is, he is a senior member of EK and a good friend, hope u got that)

3)
Ok here is another example.
Think of integer value “7”. The value of “7” won’t change in any stage of the program. I think u got it now.


4)
So a constant value in a program is denoted by a literal. That is the summary of all the bla bla things in above passages,


5)
So as I said in the 2nd paragraph the constant values or the literals can be categorized in to several groups. I think u already know about these groups and I am not going to explain much about those things.
Integer literals
Eg. 23, 545, 6556,-454

Floating point literals
Eg. 0.23, .535, -0.5343

Boolean literals
Eg. TRUE,FALSE

Character literals
Eg. ‘X’ , ‘r’ , ‘(‘

String literals
Eg. “hello world”

Hope u understand about the literals. Hee heeeeeee..may be u will think that’s all for today’s lesson. Nope. Today’s lesson is a biggy and now lets see u can remember the earlier lessons.
So what is a variable in a program?
Hmmmm…………….can u remember it. Hope u can remember that.
The variable is a memory location that stores a unit of data in a java program. So variable is defined by the combination of a identifier, a data type, and optional initializer. And all variables have a scope. I think u know about scope right. It is the domain of a variable.

Ok now we are moving on to the variable declaration. I think we did this practically in our earlier lessons. But let’s see what it is in detail.

6) Variable Declaration

So u know what is a variable, so before u use a variable in a java application u must declare it. So what is declaring?
Actually u have to introduce the variable to the java application before u use it,
E.g.
So u are going to a new company. Ok. Then u are asked to introduce u r self to the other employers in that company. So how are u going to introduce u r self. First u may be say u r name,
“I am Ravi”
Then what. You are in a company right, so secondly u are going to say in which department u are working,
“I am working in the programming department”
So like that u are going to introduce u r self in to the crowd. When u are introducing u r self in to the crowd u are not going to tell all the bla bla bla things about u. only the most important things.

So that example is about introducing a person. Similarly, we have to declare the variable
in to a java application. Like in the example u have to give the name, type and also a value for the variable. So in java “identifier” is the name of the variable. You can see the basic variable declaration form below.

type identifier [=value];


67479780am0.gif


So why u think that [=value] part is inside a square brackets, because it is optional. Ahaaaaaaaa heard about the optional initialization previously in this lesson. Check out last part of the 5th para. The thing is u can assign a value for the variable at the declaration point and also u can assign a value for that in the middle of the java code. So it is optional thing to assign a value in the beginning. Let’s get an example, and then u will understand it easily.

int x; ----------------this declares int variable x
int x=12;------------this initializes x

I think u understand about it.

So there are some important things u have to know when u are naming a variable. That means u can’t use anything for an identifier. There are three main roles u have to follow when naming an identifier,

1) The identifier may only contain simple and capital letters[a-z , A-Z] , digits [0-9], the dollar mark [$] and the underscore[ _ ]

2) An identifier cannot start with a digit.

3) Keywords cannot be used as an identifier.
(there are some keywords in java which are used to perform some activities. As an example think about “int”. “int is used to inform the data type.)

So that’s all about variable declaring. Hope u got something out of the lesson. Post u r comments and questions.
Aaaaaah forgot to say next lesson will come with a hi quality video and it’s a practical lesson.


………………Adios amigos ………………..(he heee good bye friends in spanish)
 
Last edited:

henderson

Active member
  • Nov 24, 2007
    3,150
    2
    38
    Senior Executive, Goldman Sachs
    nice lesson ravi malli, keep the good work up bro. And also remember you don't have to assign a value to field variables always and you have to assign a value to temp variables you declare inside blocks before you use them.

    Also you might want to explain the java naming convensions too.
     

    ravicplk

    Member
    Oct 18, 2007
    4,120
    1
    0
    39
    Sri Lanka
    henderson said:
    nice lesson ravi malli, keep the good work up bro. And also remember you don't have to assign a value to field variables always .

    yess,,,,,that is optional nee. we can assign values at any point with in the java application.
     

    henderson

    Active member
  • Nov 24, 2007
    3,150
    2
    38
    Senior Executive, Goldman Sachs
    ravicplk said:
    yess,,,,,that is optional nee. we can assign values at any point with in the java application.

    if the variable is temp variable you have to assign a value before using it, for example

    class A
    {
    int x;

    public void a()
    {
    int y;
    y++; --------------------(1)
    x++; --------------------(2)
    }
    }


    (1) is in correct since y does not have an assigned value
    but, (2) is correct since x is a field variable
     

    ravicplk

    Member
    Oct 18, 2007
    4,120
    1
    0
    39
    Sri Lanka
    henderson said:
    if the variable is temp variable you have to assign a value before using it, for example

    class A
    {
    int x;

    public void a()
    {
    int y;
    y++; --------------------(1)
    x++; --------------------(2)
    }
    }


    (1) is in correct since y does not have an assigned value
    but, (2) is correct since x is a field variable

    yes ayya. 1 is definitely incorrect coz the variable y is not declared nee..
    assigning values is optional..
    thanks ayya for the reply

    guys i think u got that.
     

    x-pert

    Member
    Jun 13, 2006
    20,952
    77
    0
    Nice :cool: :)

    Keep up the good work bro :D

    ravicplk said:
    So think about xpert ayya. He is the only xpert in EK. Xpert is a constant thing(rather a person) in EK. And always he represent him self, that mean that he is not a puppet like thing. So if we compare that example with a program, EK is the program and Xpert is the literal. Hmm not the best example. If u don’t understand the concept please post.ok.

    :rofl: :rofl:

    Nice example anyway :lol: