トピック
Andres TanookiOcarina

Java tutorial #2.

In this tutorial, I will cover data types and variables in Java.
4そうだね
未プレイ
返信[1]
親投稿
Andres TanookiOcarina
In the last lesson, you learned how to write a Hello World program, and you also learned some cool things that made it work. Now, we will learn about the various data types available in Java. A data type is just the type of data that can be stored in a variable, as we will see in just a moment. There are nine data types which we will go over.
0そうだね
未プレイ
返信[2]
親投稿
Andres TanookiOcarina
The first data type is called int. Int is an integer value that is 32-bit in size. Ints CANNOT be decimal numbers. They must be whole numbers. Three other data types that are similar are byte (8-bit), short (16-bit), and long (64-bit). Int is the most common out of all these types.
0そうだね
未プレイ
返信[3]
親投稿
Andres TanookiOcarina
However, you can't assign a really huge number as an int; Java indeed has its limits. Byte range: -128 to 127 inclusive. Short range: -32,768 to 32,767 inclusive. Int range: -2³¹ to 2³¹-1. Long range: -2⁶³ to 2⁶³-1.
0そうだね
未プレイ
返信[4]
親投稿
Andres TanookiOcarina
The next data types we will look at are float and double. With these data types, you can use decimals. Floats can have up to seven decimal places, while doubles can have up to sixteen. Doubles are much more accurate, especially in currency, because of its advantage of decimal places.
0そうだね
未プレイ
返信[5]
親投稿
Andres TanookiOcarina
Next one we have is the boolean. The word boolean is named after George Boole, a logician. A boolean value is simply true or false. If 5 is greater then 3, then the boolean is true. If the opposite, however, the boolean is false.
0そうだね
未プレイ
返信[6]
親投稿
Andres TanookiOcarina
The final two data types are char and String. A char is a single Unicode character. Chars must always be between single quotation marks, like this: 'a' A String is pretty much anything within two double quotation marks. It is the only data type in Java that must be defined with a capital letter. Our message "Hello world!!!!!" is an example of a string.
0そうだね
未プレイ
返信[7]
親投稿
Andres TanookiOcarina
Okay, now that we learned about the data types, let's put them to good use with variables. A variable is a placeholder for a data type that takes a name. It's just like in algebra. You have a variable called x, and the number it holds is 2. That's just how variables work in programming.
0そうだね
未プレイ
返信[8]
親投稿
Andres TanookiOcarina
Okay, let's learn how to make our own variables. Java is a "strongly-typed" language, meaning that you have to assign the data type to its correct value yourself. Let's look at the "declaration and initialization" process. <data type> <name> = <value>;
0そうだね
未プレイ
返信[9]
親投稿
Andres TanookiOcarina
The declaration part: you start by typing in the data type, then the name of the variable. The initialization part: then with the equal sign (=), you set it equal to the value. int exampleVar = 38; You can also declare and initialize on different lines: int anotherVar; anotherVar = 71; Let's define other data types into variables.
0そうだね
未プレイ
返信[10]
親投稿
Lars DespiseAdmims
Would you happen to have any experience with programming in Python?
1そうだね
未プレイ
返信[11]
親投稿
Andres TanookiOcarina
byte myByte = 50; short shrift = 66; long windingRoad; windingRoad = 245; float miiverseHateLevel; miiverseHateLevel = 999.80309; double bigDecimal = 84.12345678901; boolean thisIsSoTrue = false; char firstInitial = 'A'; String name = "Andres Saldarriaga";
0そうだね
未プレイ
返信[12]
親投稿
Andres TanookiOcarina
@ spook lrs: Yes, I do. But right now I am teaching Java.
2そうだね
未プレイ
返信[13]
親投稿
Andres TanookiOcarina
We can also use variables with System.out.println(). If you use the variable's name as the argument, like so: System.out.println(firstInitial); Then in the console, it will print out the value: A. You can also use math with variables! System.out.println(myByte + exampleVar); Which will give us 88.
0そうだね
未プレイ
返信[14]
親投稿
Andres TanookiOcarina
And you can also get the sum, difference, product, etc. of other numerical variables into one variable!! int thisMayBePrettyBig = myByte + exampleVar + windingRoad; Besides these reasons, there are so many other ways variables can be used, and are really helpful in programming. That's all for this lesson. In the next lesson, I will talk about mathematics in Java!
1そうだね
未プレイ
返信[15]
親投稿
Oscar PwnageBlock
Miiverse may not be the best place to post huge tutorials... You should make a blog and link it from here, though.
0そうだね
未プレイ
返信[16]
親投稿
Andres TanookiOcarina
I try to explain things as easy as possible. I had actually done a previous tutorial and people loved it. After all, there isn't much to do on here ever since the redesign.
1そうだね
未プレイ
返信[17]
親投稿
Andrew gamer52
why dont you post tutorials about basic instead i need help on it?
0そうだね
プレイ済み
返信[18]
親投稿
Andres TanookiOcarina
I don't have SmileBASIC, unfortunately. But with my knowledge with most of the keywords, I could be able to help you.
0そうだね
未プレイ