0% found this document useful (0 votes)
129 views4 pages

Lesson 2

Primitive data types in Java include integers, doubles, characters, and booleans. Integers are whole numbers, doubles are real numbers, characters are single character values, and booleans have true or false values. Variables must be declared with a data type and can then be assigned values of that type, such as declaring an integer variable and assigning it the value 10. Methods allow code to be organized and reused by defining blocks of code that can be called from other parts of the program.

Uploaded by

api-323529383
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views4 pages

Lesson 2

Primitive data types in Java include integers, doubles, characters, and booleans. Integers are whole numbers, doubles are real numbers, characters are single character values, and booleans have true or false values. Variables must be declared with a data type and can then be assigned values of that type, such as declaring an integer variable and assigning it the value 10. Methods allow code to be organized and reused by defining blocks of code that can be called from other parts of the program.

Uploaded by

api-323529383
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Primitive Data Types

In Java there are four commonly used primitive data types:

For this course, since we are only working on the basics of the Java, we will only be working
with ints and doubles. However, eventually you will come across char (short for characters) and
booleans. Before we go into each of the types, it is important to note that Strings are not a
primitive data type, they are Objects, but you do not need to know much about how they work or
how they are implemented.
Ints (short for integers) are any whole numbers - positive or negative. Doubles are any real
numbers. The easiest way to think of it is that doubles are any numbers with a decimal. Thus
even the number 3.0 would be considered a double. Chars are single character values. These are
different from Strings in two ways. The first is that Strings are defined with double quotes and
chars are defined with single quotes. The second way is that strings can be of infinite length
whereas chars can only be a single character. Booleans are generally used for comparison
purposes and have only two values: true or false.

Variables
Java is considered as a strongly typed programming language. Thus all variables in the Java
programming language ought to have a particular data type.
Variables and all the information they store are kept in the computer's memory for access. Think
of a computer's memory as a table of data where each cell corresponds to a variable.
Upon creating a variable, we basically create a new address space and give it a unique name.
Java goes one step further and lets you define what you can place within the variable in Java
parlance you call this a data type. So, you essentially have to do two things in order to create a
variable:

Create a variable by giving it a unique name; and,


Define a data type for the variable.

The following code demonstrates how a simple variable can be created. This process is known as
variable declaration.
int a;

Assigning values to variables[edit]


Because we have provided a data type for the variable, we have a hint as to what the variable can
and cannot hold. We know that int (integer) data type supports numbers that are either positive or
negative integers. Therefore once a variable is created, we can provide it with any integer value
using the following syntax. This process is called an assignment operation.
int a = 10;
When naming identifiers, you need to use the following guidelines which ensure that your
variables are named accurately. We should always name our variables in a way that tells us what
they hold. Now you can tell what's happening, can't you? However, before we continue, notice
the case of the variables. If a word contains CAPITAL LETTERS, it is in UPPER CASE. If a
word has small letters, it is in lower case. Both cases in a word renders it as mIxEd CaSe.
The variables we studied so far had a mixed case. When there are two or more words making up
the names of a variable, you need to use a special case called thecamel-case. Just like the humps
of a camel, your words need to stand out. Using this technique, the words first and name could
be written as either firstNameor FirstName.
The first instance, firstName is what we use as the names of variables. Remember though,
firstName is not the same as FirstName because Java is case-sensitive. Case-sensitive basically
implies that the case in which you wrote one word is the case you have to call that word in when
using them later on. Anything other than that is not the same as you intended. You'll know more
as you progress. You can hopefully tell now why the variables you were asked to identify weren't
proper.

Operators and Variables


How to write Methods:
So far weve already seen one type of method in Java, the main method. This is the only method
that Java looks at when running the program (keep this in mind).

You might wonder, what is the point of creating other methods when all Java looks at is the main
method. There are two reasons.
1) In programming, there is generally a lot of repetitiveness. Just as in other things,
we want to remove all instances of repetitiveness and shorten it down. This is why we
condense things into methods. By doing so, we can limit the number of lines of our code
and make it more readable.
2) If we wrote every single line of code in our main method, over time it would
become hard to read - even if the code is not repetitive. For example, if I wrote this lesson
plan all under one category, it would just be one giant jumble of words. By distributing it
up into sections, it is easier for you guys to read.
This is why it is good practice to create new methods, even if the program you are writing right
now is simple. It helps create better habits for when you write larger code.
The syntax for other methods is similar to that of the main method. Your header will be:
public static void <name> (){
}
Instead of <name> input whatever you want to name your method. You want to stick with
naming conventions for methods which are that the first word of the name is lowercase and every
word after that is capitalized. For example: public static void firstMethod() { }
You want to make sure that this method is declared outside of the main method because youre
creating a new one. So make sure it is outside the end bracket of the main method.
Another thing is, for now, our parentheses will not have anything within them (recall how the
main method had String[] args inside of the parentheses). We will learn about changing that in
Lesson 3, but for now you can work with empty parentheses.
Now you can do anything that you do in the main method in the new method that you created!
For example, try running this code:
public class Intro{
public static void main(String[] args){
}
public static void firstMethod (){
System.out.print(Hello World!);
}
}
Now you may notice that the terminal didnt output anything. Take a look at the next section to
see why.

Calling Methods

Remember in the previous section we talked about how Java only looks at the main method
when it runs, this is why your code didnt output anything. Java compiled your code with no
errors but when you tried to run it, it looked in the main method and found nothing. So your code
did do what it was supposed to, which was nothing. In order to have Java look at this other
method you wrote and execute its body, you have to call it. You can do this by simply writing the
name of the method and the parenthesis in the main method (similar to writing an initialization or
assignment statement for a variable).
public class Intro{
public static void main(String[] args){
firstMethod();
}
public static void firstMethod (){
System.out.print(Hello World!);
}
}
Now if you try compiling and running this, it should print Hello World! .

You might also like