tag:blogger.com,1999:blog-179769192115108692024-02-08T08:33:36.038-08:00I want to programTutorial to start learing to program. I will made tutorial for some languages.Sergio Lemahttp://www.blogger.com/profile/08414888476925050297noreply@blogger.comBlogger19125tag:blogger.com,1999:blog-17976919211510869.post-8339069004889101192012-06-18T13:00:00.000-07:002012-06-18T14:25:14.216-07:00Buffered reader<div style="text-align: justify;">
In many times you will read data from some device or file, like a keyboard, a serial port or a txt file. For all those devices, you will need a BufferedReader object. This object allows you to put in all the information you want to read, and then get it by characters or lines. Whatever you need to read, you can pass it to the BufferedReader when you instancied it, and then you can read its data when you need. Here is an example:</div>
<blockquote class="tr_bq">
System.out.println("Write something you want to read., and press Enter.");<br />
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));<br />
String data = keyboard.readLine();</blockquote>
<div style="text-align: justify;">
In the previous code I use the InputStreamReader to get access to the keyboard (System.in). I will explain it more deeply in another article. When the "readLine()" method is call, the execution of the code will stop there until the user enter some data in the console and press Enter. The user must press Enter to finish the line, because the ejecution will not continue until there is a complete line in the BufferedReader. All the data written in the console from the keyboard will be stored in the variable "data" as a string.<br />
<br />
Lets examine the different method of the BufferedReader. The BufferedReader can read its content line by line, as it was shown before, but it can read it char by char or a specified number of characters. Another way is specifiyng a token or mark. When you read char by char, the mark will move ahead. Then you can skip some characters you don't want to read, or you can reset this mark to the beginning of the buffer. Here are some examples:<br />
<br />
File.txt:<br />
<blockquote class="tr_bq">
Hello, this is a file.<br />
There are many lines in this file.<br />
But this is the last one.</blockquote>
<br />
Code:<br />
<blockquote class="tr_bq">
String a; char[] b = new char[10]; int c;<br />
BufferedReader file = new BufferedReader(new FileReader("File.txt"));<br />
a = file.readLine();<br />
System.out.println("First read: " + a);<br />
c = file.read(b, 0, 10);<br />
System.out.println("Second read, " + c + " characters read: " + new String(b));<br />
file.reset();<br />
file.skip(7);<br />
a = file.readLine();<br />
System.out.println("Last read: " + a);</blockquote>
<br />
Output:<br />
<blockquote class="tr_bq">
First read: Hello, this is a file.<br />
Second read, 10 characters read: There are<br />
Last read: this is a file.</blockquote>
Reading something with a BufferedReader can throw some exception, so every time the read methods must be inside a try/catch clause.</div>Sergio Lemahttp://www.blogger.com/profile/08414888476925050297noreply@blogger.com1tag:blogger.com,1999:blog-17976919211510869.post-19915421347446647792012-06-17T13:30:00.001-07:002012-06-18T14:25:28.212-07:00Console prints<br />
In this seccion, we are going to studie how to print messages in the console. There is two message types which can be printed in console, the error messages and the rest. The error messages normally appears with another style, in red for example, specially if you use an IDE programm to launch your application. Here is an example of the two types:<br />
<blockquote class="tr_bq">
System.out.println("This is an info message");<br />
System.err.println("This is an error message");</blockquote>
<br />
Both message types owns to the System class, and have the same methods to print messages. Basically, you can print in a single line (print), or have a return line at the end of your message (println). Here are some examples:<br />
<blockquote class="tr_bq">
System.out.print("The next message of this one will be printed in the same line.");<br />
System.out.println(" This message will be printed in the same line as the previous one.");<br />
System.out.println("This will be in the second line.");</blockquote>
<br />
Output:<br />
<blockquote class="tr_bq">
The next message of this one will be printed in the same line. This message will be printed in the same line as the previous one.<br />
This will be in the second line.</blockquote>
<br />
In the case of the prints in the same line, you must take care of the spaces between the messages.<br />
<br />
To print the values of variables in the middle of messages, you must cut the message where you want to put a variable, and then write the name of the variable inside. To cut a message you only have to close the accuotations and add the + signal to add some other stuff you want to print, like another message, variable values... Here are some examples:<br />
<blockquote class="tr_bq">
int a = 5;<br />
System.out.println("The variable a = " + a);<br />
System.out.println("Message one " + "followed by " + "Message two");</blockquote>
<br />
Output:<br />
<blockquote class="tr_bq">
The variable a = 5<br />
Message one followed by Message two</blockquote>
<br />
The variable you want to print must be inicialized. And to print arrays or list, you must specifie which element one the collection you want to print, otherwise the direction of the object will be printed. And if the variable is not inicialized, a NullPointerException will be thrown. Here is an example:<br />
<blockquote class="tr_bq">
int[] a = new int[5];<br />
a[0] = 1; a[1] = 2; a[2] = 2; a[3] = 5; a[4] = 7;<br />
System.out.println("The forth element of the array is " + a[3]);</blockquote>
<br />
Output:<br />
<blockquote class="tr_bq">
The forth element of the array is 5</blockquote>Sergio Lemahttp://www.blogger.com/profile/08414888476925050297noreply@blogger.com0tag:blogger.com,1999:blog-17976919211510869.post-32488523072999776682012-06-13T23:05:00.002-07:002012-06-18T14:25:40.900-07:00Main<br />
<div style="text-align: justify;">
To start some application in Java, you must implement the main method. This method is the one who start your application. You can have many main methods implemented, but when you are going to compile your application you must specified which one of them must be executed. The main method must be static, that means it can not own to any class (it is a little ambiguous, because it must be implement inside a class), and can have many arguments as strings.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Here is an example:</div>
<blockquote class="tr_bq">
public static void main(String[] args){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>if (args.length == 0)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("No argument was specified");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>else if (args.length == 1)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("One argument was specified: "+args[0]);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>else<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Many arguments were specified!");<br />
}</blockquote>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Those arguments can be used to create internal classes or set variables.</div>Sergio Lemahttp://www.blogger.com/profile/08414888476925050297noreply@blogger.com0tag:blogger.com,1999:blog-17976919211510869.post-43603090148377440762012-06-12T22:58:00.001-07:002012-06-18T14:25:56.590-07:00Collections<br />
<div style="text-align: justify;">
The collections are very similar to arrays, in fact there are special arrays. There are many collections (ArrayList, HashMap, Vector...) each one has different properties. Everyone must be instancied, and have common methods to get and set their values. Someone has different methods to set its values (like Map, HashMap, HashTable...) because every set of value is composed by a key and a value.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Every collection must specified the data type there are going to store. The data type must be specified when the variable is declared, and must be the same when it is instancied:</div>
<blockquote class="tr_bq">
ArrayList<String> a;<br />
a = new ArrayList<String>();</blockquote>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
The collections do not need to specified its size, they have a dynamic size, you can put some values and the size of the collections will increase at the time. When you instancied a collection, you can specified some properties, like the initial size, and the value which will increase the collection at each insertion:</div>
<blockquote class="tr_bq">
Vector<Integer> a = new Vector<Integer>(0,1); //initial size=0; increment value=1</blockquote>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Like I said before, the collections have common methods to access their values:</div>
<div style="text-align: justify;">
</div>
<ul>
<li><b>boolean add(Object o)</b>: add a new element at the end of the collection</li>
<li><b>boolean add(int index, Object o)</b>: add a new element at the specified index</li>
<li><b>boolean addAll(Collection<Object> c)</b>: add all the given collection to the end of this collection</li>
<li><b>void clear()</b>: delete all the elements</li>
<li><b>boolean contains(Object o)</b>: return true if the collection has the given object</li>
<li><b>boolean containsKey(Object key)</b>: return true if the collection has the given object as a key</li>
<li><b>boolean containsValue(Object value)</b>: return true if the collection has the given object as a value</li>
<li><b>Object get(int index)</b>: return the object stored in the given index</li>
<li><b>int getIndexOf(Object o)</b>: return the index where is stored the given object</li>
<li><b>int lastIndexOf(Object o)</b>: return the index where is stored the given object starting to look at the end of the collection</li>
<li><b>Object put(Object key, Object value)</b>: put a set of key/value in the collection</li>
<li><b>Object remove(int index)</b>: remove the object stored at the given index</li>
<li><b>boolean remove(Object o)</b>: remove the given object from the collection, and returns true if it was successfull</li>
<li><b>int size()</b>: return the size of the collection</li>
</ul>
<br />
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Here are not shown all the methods, and not every of those methods are in every collection, each collection has its own properties. Here are explained to most commons:</div>
<div style="text-align: justify;">
</div>
<ul>
<li><b>ArrayList</b> and <b>Vector</b>: those are both simples arrays. Every data you add will be stored at the end of the collection. The difference between them are very insignificant: The Vector store its values synchronizing multiples threads, and ArrayList does not. Internally, the ArrayList size is smaller than the Vector's.</li>
<li><b>HashMap</b> and <b>HashTable</b>: those are a two dimensional arrays. Every value is defined from a key, not from an index like the previous one. The new values in those arrays are not stored at the end of the collection, but in the first place of internal memory. To get one of the stored values, you must give the key, and viceversa. The difference between HashMap and HashTable are similar to ArrayList and Vector. HashTable stores its values synchronizing multiples threads, and HashMap does not. But HashMap allows store null values, and only one null key, and HashTable does not store any null value (neither key or value).</li>
</ul>
<br />Sergio Lemahttp://www.blogger.com/profile/08414888476925050297noreply@blogger.com0tag:blogger.com,1999:blog-17976919211510869.post-31914897124699193212012-06-12T03:16:00.002-07:002012-06-18T14:26:08.228-07:00Arrays<br />
<div style="text-align: justify;">
In the Java language, the arrays are quite simple. You must instanciate it before operate with, and specifie its size. Then you can set a value to each index. Every index must be set as the original data type, if it must to be instancied, here it must also be instancied.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Here is an example:</div>
<blockquote class="tr_bq">
byte[] a = new byte[3];<br />
a[0] = 3;<br />
a[1] = 3;<br />
a[2] = 4;<br />
Object[] b = new Object[2];<br />
b[0] = new Object();<br />
b[1] = new Object();</blockquote>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
If you want to copy an array, there is a method in the System class which can made it for you:</div>
<blockquote class="tr_bq">
byte[] a = new byte[2];<br />
byte[] b = new byte[2];<br />
a[0] = 1;<br />
a[1] = 2;<br />
System.arrayCopy(a, 0, b, 0, a.length);</blockquote>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
The method System.arrayCopy(...) take the values from a source array to a destination array. You must specifie the initial position where to start reading in the source array, and the initial position where to start writting in the destination array, then the number of elements to copy must be also specified. Both arrays must be intancied before call this method.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
The arrays has some properties, and one of them is to know the size of the array, as it was made in the previous example. When you create an array, you do not know how the size will be in the future, so you can use this method when you check the content in a loop. This is very important because if you access to a direction of an array bigger than its size, an exception will be thrown.</div>Sergio Lemahttp://www.blogger.com/profile/08414888476925050297noreply@blogger.com0tag:blogger.com,1999:blog-17976919211510869.post-67756487612268442642012-06-10T11:01:00.006-07:002012-06-18T14:26:27.214-07:00While loops<br />
<div style="text-align: justify;">
In this lesson we are going to learn about the while and do-while loop. Those loops have not a defined initial number of cicles to do. They will execute the code inside the loop until the condition is false. The difference betwenn the do-while loop and the while loop is that the first one will execute the code inside its loop almost one time, as the while loop will look first if its condition is right to execute its code.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Example:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
int a = 5, b = 3;<br />
//this loop will never be executed<br />
while (a < 5){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>a--;<br />
}<br />
//this will be execute until a is less or equal than 5<br />
do{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>a--;<br />
}while(a > 5);</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>Sergio Lemahttp://www.blogger.com/profile/08414888476925050297noreply@blogger.com0tag:blogger.com,1999:blog-17976919211510869.post-20528818132903705322012-06-10T11:01:00.003-07:002012-06-18T14:26:39.129-07:00For loops<br />
The for loops are used when the number of times you want the loop to be executed is previously defined. The for loop has a block of code which is executed every loop, and the conditional sentence to do the loops. The conditional sentence is divided is three parts: the initial value, the condition to stay in the loop and the action to do every loop. Each of those three fields are optional, but be carefull because you can convert it in an inifinitive loop.<br />
The initial value is where you can create a variable and indicate its initial value. There, you can also use an existing value and set it to another value.<br />
The condition to stay in the loop is set in the second field of the for loops. This indicate how many times will the code of the for loop be executed. The most common condition for this field is a counter, but any condition can be used. The parameters used here can be either modified int he third field or in the code of the for block. Basically, the condition waits for a true or false return.<br />
The last field, is the action which execute the for loop at every loop. This action is most of the times incrementing a counter, but as I said before, it is an optional field, so the counter can be incremented inside the for loop. If the condition waits to the change of a parameter, then this field can be ommited.<br />
<br />
Here is an example:<br />
<blockquote class="tr_bq">
for(int i=0; i<4; i++){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("numeber=" + i);<br />
}</blockquote>
<br />
Output:<br />
<blockquote class="tr_bq">
number=0<br />
number=1<br />
number=2<br />
number=3</blockquote>
<br />
Another example:<br />
<blockquote class="tr_bq">
int a = 0;<br />
boolean exit = false;<br />
for(;!exit;){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>if (a == 4){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>a++;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("We still inside the loop");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>else{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>exit = true;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("We are going out the loop");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
}</blockquote>
<br />
Output:<br />
<blockquote class="tr_bq">
We still inside the loop<br />
We still inside the loop<br />
We still inside the loop<br />
We still inside the loop<br />
We are going out the loop</blockquote>
<br />
The condition field can also be ommited, but in this case, we must do something to get out from the for loop. One instruction to get out of a loop, is simply a return instruction. In this case we go out of the for loop and the method. Another instruction is the break instruction. This instruction can be used to go out of any loop or a block of code (do-while, while, for, switch...).Sergio Lemahttp://www.blogger.com/profile/08414888476925050297noreply@blogger.com0tag:blogger.com,1999:blog-17976919211510869.post-62042867667354229532012-06-10T11:00:00.005-07:002012-06-18T14:26:50.813-07:00If conditions<br />
<div style="text-align: justify;">
In this lesson we are going to learn how to use the If coniditions. The If condition is used to decide where the execution of the code must go. Inside the If condition the is a logical condition which decide if the code will be executed inside the If condition or if it goes out. Then, the If condition can have an Else-If to catch the next execution, or it can have a final Else to execute the default code.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Example:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
int a = 3, b = 4;<br />
if (a == b){<span class="Apple-tab-span" style="white-space: pre;"> </span>//the following code will not be executed<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("a is equal to b");<br />
}<br />
else if (a > b){<span class="Apple-tab-span" style="white-space: pre;"> </span>// the following code will be executed<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("a is greater than b");<br />
}<br />
else<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("a is less than b");<br />
/* the code before will not be executed. By the way, this code is considered inside the else, although not having braces. In those cases, only the next sentence will be executed */</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>Sergio Lemahttp://www.blogger.com/profile/08414888476925050297noreply@blogger.com0tag:blogger.com,1999:blog-17976919211510869.post-81183574158883338252012-06-10T11:00:00.002-07:002012-06-18T13:03:25.921-07:00Maths<br />
<div style="text-align: justify;">
In this lesson we are going to learn the basic arithmetic operations we can do in Java.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Symbol<span class="Apple-tab-span" style="white-space: pre;"> </span>Operation</div>
<div style="text-align: justify;">
+<span class="Apple-tab-span" style="white-space: pre;"> </span>Adition</div>
<div style="text-align: justify;">
-<span class="Apple-tab-span" style="white-space: pre;"> </span>Subtraction</div>
<div style="text-align: justify;">
*<span class="Apple-tab-span" style="white-space: pre;"> </span>Multiplication</div>
<div style="text-align: justify;">
/<span class="Apple-tab-span" style="white-space: pre;"> </span>Division</div>
<div style="text-align: justify;">
%<span class="Apple-tab-span" style="white-space: pre;"> </span>Remainder</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Examples:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
a = 5 + 3;<span class="Apple-tab-span" style="white-space: pre;"> </span>//a = 8<br />
b = a - 3;<span class="Apple-tab-span" style="white-space: pre;"> </span>//b = 5<br />
c = b * 2;<span class="Apple-tab-span" style="white-space: pre;"> </span>//c = 10<br />
d = c / 2;<span class="Apple-tab-span" style="white-space: pre;"> </span>//d = 5<br />
e = d % 2;<span class="Apple-tab-span" style="white-space: pre;"> </span>//e = 1</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Unary operations:</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Symbol<span class="Apple-tab-span" style="white-space: pre;"> </span>Operation</div>
<div style="text-align: justify;">
++<span class="Apple-tab-span" style="white-space: pre;"> </span>Increments a value by one</div>
<div style="text-align: justify;">
--<span class="Apple-tab-span" style="white-space: pre;"> </span>Decrements a value by one</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
For those one, the variable can be before or following the symbol, but the operation will be different. Here is an example:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
int a = 0;<br />
a++;<span class="Apple-tab-span" style="white-space: pre;"> </span>// a = 1<br />
++a;<span class="Apple-tab-span" style="white-space: pre;"> </span>// a = 2<br />
System.out.println(a++); // print 3<br />
System.out.println(++a); // print 4 but a = 3. The instruction prints without changing the value of the variable.</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Another form of this use is the following:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
int a = 0;<br />
a = +1;<span class="Apple-tab-span" style="white-space: pre;"> </span>//it increments the value of the variable "a".</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Logical Operations:</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Symbol<span class="Apple-tab-span" style="white-space: pre;"> </span>Operation</div>
<div style="text-align: justify;">
==<span class="Apple-tab-span" style="white-space: pre;"> </span>Equal to</div>
<div style="text-align: justify;">
!=<span class="Apple-tab-span" style="white-space: pre;"> </span>Not equal to</div>
<div style="text-align: justify;">
><span class="Apple-tab-span" style="white-space: pre;"> </span>Greater than</div>
<div style="text-align: justify;">
>=<span class="Apple-tab-span" style="white-space: pre;"> </span>Greater than or equal to</div>
<div style="text-align: justify;">
<<span class="Apple-tab-span" style="white-space: pre;"> </span>Less than</div>
<div style="text-align: justify;">
<=<span class="Apple-tab-span" style="white-space: pre;"> </span>Less than or equal to</div>
<div style="text-align: justify;">
&&<span class="Apple-tab-span" style="white-space: pre;"> </span>Conditional AND</div>
<div style="text-align: justify;">
||<span class="Apple-tab-span" style="white-space: pre;"> </span>Conditional OR</div>
<div style="text-align: justify;">
!<span class="Apple-tab-span" style="white-space: pre;"> </span>Inverts the value of a boolean</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Examples:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
int a = 5, b = 3;<br />
return (a == b);<span class="Apple-tab-span" style="white-space: pre;"> </span>//return false<br />
return (a != b);<span class="Apple-tab-span" style="white-space: pre;"> </span>//return true<br />
return (a > b);<span class="Apple-tab-span" style="white-space: pre;"> </span>//return true<br />
return (a >= b);<span class="Apple-tab-span" style="white-space: pre;"> </span>//return true<br />
return (a < b);<span class="Apple-tab-span" style="white-space: pre;"> </span>//return false<br />
return (a <= b);<span class="Apple-tab-span" style="white-space: pre;"> </span>//return false<br />
return ((a == b) && (a != b));<span class="Apple-tab-span" style="white-space: pre;"> </span>//return false<br />
return ((a == b) || (a != b));<span class="Apple-tab-span" style="white-space: pre;"> </span>//return true<br />
return !(a == b);<span class="Apple-tab-span" style="white-space: pre;"> </span>//return true</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Bitwise and bit shift operations:</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Symbol<span class="Apple-tab-span" style="white-space: pre;"> </span>Operations</div>
<div style="text-align: justify;">
~<span class="Apple-tab-span" style="white-space: pre;"> </span>Unary bitwise complement</div>
<div style="text-align: justify;">
<<<span class="Apple-tab-span" style="white-space: pre;"> </span>Signed left shift</div>
<div style="text-align: justify;">
>><span class="Apple-tab-span" style="white-space: pre;"> </span>Signed right shift</div>
<div style="text-align: justify;">
>>><span class="Apple-tab-span" style="white-space: pre;"> </span>Unsigned right shift</div>
<div style="text-align: justify;">
&<span class="Apple-tab-span" style="white-space: pre;"> </span>Bitwise AND</div>
<div style="text-align: justify;">
^<span class="Apple-tab-span" style="white-space: pre;"> </span>Bitwise exclusive OR</div>
<div style="text-align: justify;">
|<span class="Apple-tab-span" style="white-space: pre;"> </span>Bitwise inclusive OR</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Examples:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
int a = 2;<span class="Apple-tab-span" style="white-space: pre;"> </span>// <span class="Apple-tab-span" style="white-space: pre;"> </span>binary value: a = 00000000 00000000 00000000 00000010<br />
a = ~a;<span class="Apple-tab-span" style="white-space: pre;"> </span>// a = -3, <span class="Apple-tab-span" style="white-space: pre;"> </span>binary value: a = 11111111 11111111 11111111 11111101<br />
a = a << 2;<span class="Apple-tab-span" style="white-space: pre;"> </span>// a = -12, <span class="Apple-tab-span" style="white-space: pre;"> </span>binary value: a = 11111111 11111111 11111111 11110111<br />
a = a >> 2;<span class="Apple-tab-span" style="white-space: pre;"> </span>// a = -3<span class="Apple-tab-span" style="white-space: pre;"> </span>binary value: a = 11111111 11111111 11111111 11111101<br />
a = a >>> 2;<span class="Apple-tab-span" style="white-space: pre;"> </span>// a = 1073741823<span class="Apple-tab-span" style="white-space: pre;"> </span>binary value: a = 01111111 11111111 11111111 11111111<br />
a = a & 2;<span class="Apple-tab-span" style="white-space: pre;"> </span>// a = 2<span class="Apple-tab-span" style="white-space: pre;"> </span>binary value: a = 00000000 00000000 00000000 00000010<br />
a = a ^ 2;<span class="Apple-tab-span" style="white-space: pre;"> </span>// a = 0<span class="Apple-tab-span" style="white-space: pre;"> </span>binary value: a = 00000000 00000000 00000000 00000000<br />
a = a | 2;<span class="Apple-tab-span" style="white-space: pre;"> </span>// a = 2<span class="Apple-tab-span" style="white-space: pre;"> </span>binary value: a = 00000000 00000000 00000000 00000010</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>Sergio Lemahttp://www.blogger.com/profile/08414888476925050297noreply@blogger.com0tag:blogger.com,1999:blog-17976919211510869.post-92155272153850800942012-06-10T10:59:00.003-07:002012-06-19T10:41:15.314-07:00Enum classes<br />
<div style="text-align: justify;">
The enum classes are used to enumerated multiple secuential values with its names. Those values are statics, and can be accessed publicly. </div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Example:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
public enum Week{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>MONDAY,<span class="Apple-tab-span" style="white-space: pre;"> </span>//numeric value = 0<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>TUESDAY,<span class="Apple-tab-span" style="white-space: pre;"> </span>//numeric value = 1<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>WEDNESDAY,<span class="Apple-tab-span" style="white-space: pre;"> </span>//numeric value = 2<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>THURSDAY,<span class="Apple-tab-span" style="white-space: pre;"> </span>//numeric value = 3<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>FRIDAY,<span class="Apple-tab-span" style="white-space: pre;"> </span>//numeric value = 4<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>SATURDAY,<span class="Apple-tab-span" style="white-space: pre;"> </span>//numeric value = 5<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>SUNDAY<span class="Apple-tab-span" style="white-space: pre;"> </span>//numeric value = 6<br />
}</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Apart of this, the enum class can have variables and methods as other classes. When there are some variables in the enum class, those variables will be linked to the enum values. Thus, the enum values can be setted as you want. Here is another example:<br />
<br />
Code:<br />
<blockquote>
public enum Bottle{<br /> LITTLE (250), MEDIUM (500), BIG (1500);<br /> private int ml;<br /> public Bottle (int size){<br /> this.ml = size;<br /> }<br /> public int getSize(){<br /> return this.ml;<br /> }<br />}<br />public class Pack{<br /> private int nBottles;<br /> private Bottle bottleSize;<br /> public Pack (int nBottles, Bottle bottleSize){<br /> this.nBottles = nBottles;<br /> this.bottleSize = bottleSize;<br /> }<br /> public int getNBottles(){ return this.nBottles; }<br /> public Bottle getBottleSize() { return this.bottleSize; }<br /> public static void main(String[] args){<br /> Pack p = new Pack(6, Bottle.BIG);<br /> System.out.println("This is a pack of " + p.getNBottles() + " bottles.");<br /> System.out.println("Each " + p.getBottleSize() + " bottle has " + p.getBottleSize().getSize() + " ml.");<br /> }<br />}</blockquote>
Output:<br />
<blockquote class="tr_bq">
This is a pack of 6 bottles.<br />Each BIG bottle has 1500 ml.</blockquote>
</div>Sergio Lemahttp://www.blogger.com/profile/08414888476925050297noreply@blogger.com0tag:blogger.com,1999:blog-17976919211510869.post-32969498984958896732012-06-10T10:58:00.005-07:002012-06-18T14:27:17.462-07:00Interface classes<br />
<div style="text-align: justify;">
The interface classes are more like a template of a class. The interface show how must be the subclass, which methods must implements, those methods must not be implemented here but in the subclasses Those classes are used to organize a serie of future classes which will act all together. It is like the guidelines all the subclasses must follow. Here is an example:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
public interface CompanyInterface{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void buySupplies();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void paySalaries();<br />
}<br />
public class Supermarket implements CompanyInterface{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void buySupplies(){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Buying fruits...");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>pubilc void paySalaries(){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Paying cashiers...");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
}<br />
public class Garage implements CompanyInterface{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void buySupplies(){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Buying tyres...");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>pubilc void paySalaries(){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Paying mechanics...");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
}</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>Sergio Lemahttp://www.blogger.com/profile/08414888476925050297noreply@blogger.com0tag:blogger.com,1999:blog-17976919211510869.post-20976961104065432512012-06-10T10:58:00.002-07:002012-06-18T14:27:29.990-07:00Abstract classes<br />
<div style="text-align: justify;">
An abstract class can be defined as the beginnig of a class. It implements abstract methods that can be used and modified in subclasses which extends the abstract one. The abstract methods do not have a body, the body must be implemented in the subclasses. An example can be the following: a first abstract class is Shape, the second one y Square which extends the first one. Then the Square class can use the methods and variables of the Shape class. Not all the abstract methods must be implemented. Here is an example:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
public abstract class Shape{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public abstract void setName(String name);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public abstract void setNumberOfFaces(int faces);<br />
}<br />
public class Square extends Shape{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private String name;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void setName(String name){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>this.name = name;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
}<br />
public class Cube extends Shape{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private String name;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private int faces;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void setName(String name){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>this.name = name;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void setNumberOfFaces(int faces){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>this.faces = faces;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
}</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>Sergio Lemahttp://www.blogger.com/profile/08414888476925050297noreply@blogger.com0tag:blogger.com,1999:blog-17976919211510869.post-32516782676794562342012-06-10T10:57:00.004-07:002012-06-18T14:27:41.627-07:00Methods<br />
<div style="text-align: justify;">
In this lesson we are going to study the methods. The methods becomes from the classes. They can change some propeties of the classes or the can obtain some value or functionalities of a class. Every method can have one or more arguments passed by the user, and can return a value or not. The methods can be public, private or protected. A public method can be called from a main programm where the class is constructed. A private method can only be called in the owner class. And the protected methods are method that can be called internally and from any subclass method. The subclasses will be discussed in another lesson. The variables can also be public, private or protected. Both methods and variables can be defined without specifying the visibility, here they will take the default visibility, private.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Structure of a method:</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
[visibility] [data returned] [name] ([data type of argument 1] [name of argument 1], [data type of argument 2] [name of argument 2]...){</div>
<div style="text-align: justify;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>[...]</div>
<div style="text-align: justify;">
}</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Example:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
public int getInteger(int argument1){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>//something<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return 1;<br />
}</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
The constructor are methods too, but they are called only when the class is instancied. We can differenciate it from other methods because the constructor must have the same name as the class and it do not return anything. One class can have more than one constructor but they must be different by anyone of them from the arguments they have.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Here is an example:</div>
<div style="text-align: justify;">
<blockquote>
public class ObjectA{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>//variables<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private String v1;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private int v2;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>//constructor 1<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public ObjectA(){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>this.v1 = "Hello";<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>this.v2 = 0;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>//constructor 2<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public ObjectA(String arg1, int arg2){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>this.v1 = arg1;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>this.v2 = arg2;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>//public method<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public int getV2Incremented(){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>this.v2 = this.increment(this.v2);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return this.v2;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>//private method<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private int increment(int value){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return value++;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
}</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>Sergio Lemahttp://www.blogger.com/profile/08414888476925050297noreply@blogger.com0tag:blogger.com,1999:blog-17976919211510869.post-82143750363568903442012-06-10T10:56:00.002-07:002012-06-18T14:27:54.938-07:00Classes<br />
<div style="text-align: justify;">
Here we are going to explain the definition of a class. A class is an object that could be used in a main programm. This class has usually properties and methods. The properties can be modified by the user with the gets and sets methods. And the methods are used to get some results related to the class. This can be better explained with an example:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
public class ObjectA {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>//private properties<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>int property1;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>int property2;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>//constructor<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public ObjectA(){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>this.property1 = 0;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>this.property2 = 0;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>//gets and sets methods<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public int getProperty1(){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return this.property1;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void setProperty1(int property1){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>this.property1 = property1;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public int getProperty2(){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return this.property2;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void setProperty2(int property2){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>this.property2 = property2;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>//another method<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void increment(){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>this.property1++;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>this.property2++;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
}</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Now I am going to explain the example. First there is a class called ObjectA, which has to properties (property1 and property2). The properties can be accessed and modified by the get and set methods. Thus there is anohter method "increment" which increment the value of the variables property1 and property2 in one value. And finally, there is the constructor. The constructor is needed to create a local copy of the class ObjectA. In the constructor we can initialize the variables and call private methods if it is necessary.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
There are multiple types of classes: enum, abstract, interface or class. All must be public if they are alone in the file. If anyone is inside another, it can be public or private. The main class of the file must be named as the file.</div>
<div style="text-align: justify;">
<br /></div>Sergio Lemahttp://www.blogger.com/profile/08414888476925050297noreply@blogger.com0tag:blogger.com,1999:blog-17976919211510869.post-17501395567096457402012-06-10T10:55:00.004-07:002012-06-18T14:28:08.320-07:00Comments<br />
<div style="text-align: justify;">
In this lesson, we are going to see the comments in the code. All in the code we can put some comments to explain what is doing the programm at any time. The comments can be line by line(//), or multiple lines(/* */).</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Here is an example:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
//comment in a single line<br />
/* closed comment in a single line */<br />
/**<br />
* comment in multiple lines<br />
*/</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
The comments of multiple lines can also be closed in the same line, letting write more code following the comment. Another type of comments are the JavaDoc (/** */). Those comments are used to introduce a class, method or variable that can be seen in an IDE when you are writting some code. Here is an example of a JavaDoc for a method:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
/**<br />
* Description of the method<br />
* @param value Description of the argument named value<br />
* @return Description of the returned value<br />
*/<br />
public int setVariable(int value){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>//something<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return x;<br />
}</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>Sergio Lemahttp://www.blogger.com/profile/08414888476925050297noreply@blogger.com0tag:blogger.com,1999:blog-17976919211510869.post-2268261520305285272012-06-10T10:54:00.005-07:002012-06-18T14:28:20.944-07:00Data types<br />
<div style="text-align: justify;">
In this lesson I am going to explain the basic data types that can be used in Java. Here are the data types with its lenghts:</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Type<span class="Apple-tab-span" style="white-space: pre;"> </span>Lenght<span class="Apple-tab-span" style="white-space: pre;"> </span>Default value<span class="Apple-tab-span" style="white-space: pre;"> </span>Examples value<span class="Apple-tab-span" style="white-space: pre;"> </span>Min/Max values</div>
<div style="text-align: justify;">
boolean<span class="Apple-tab-span" style="white-space: pre;"> </span>1<span class="Apple-tab-span" style="white-space: pre;"> </span>false<span class="Apple-tab-span" style="white-space: pre;"> </span>false/true<span class="Apple-tab-span" style="white-space: pre;"> </span>-</div>
<div style="text-align: justify;">
byte<span class="Apple-tab-span" style="white-space: pre;"> </span>8<span class="Apple-tab-span" style="white-space: pre;"> </span>0<span class="Apple-tab-span" style="white-space: pre;"> </span>1, 2, 3<span class="Apple-tab-span" style="white-space: pre;"> </span>-128 / 127</div>
<div style="text-align: justify;">
short<span class="Apple-tab-span" style="white-space: pre;"> </span>16<span class="Apple-tab-span" style="white-space: pre;"> </span>0<span class="Apple-tab-span" style="white-space: pre;"> </span>1, 2, 3<span class="Apple-tab-span" style="white-space: pre;"> </span>-32,768 / 32767</div>
<div style="text-align: justify;">
int<span class="Apple-tab-span" style="white-space: pre;"> </span>32<span class="Apple-tab-span" style="white-space: pre;"> </span>0<span class="Apple-tab-span" style="white-space: pre;"> </span>1, 2, 3<span class="Apple-tab-span" style="white-space: pre;"> </span>-2,147,483,648 / 2,147,483,647</div>
<div style="text-align: justify;">
long<span class="Apple-tab-span" style="white-space: pre;"> </span>64<span class="Apple-tab-span" style="white-space: pre;"> </span>0L<span class="Apple-tab-span" style="white-space: pre;"> </span>1, 2, 3<span class="Apple-tab-span" style="white-space: pre;"> </span>-9,223,372,036,854,775,808 / 9,223,372,036,854,775,807</div>
<div style="text-align: justify;">
float<span class="Apple-tab-span" style="white-space: pre;"> </span>32<span class="Apple-tab-span" style="white-space: pre;"> </span>0.0f<span class="Apple-tab-span" style="white-space: pre;"> </span>1.1, 1.2, 1.3<span class="Apple-tab-span" style="white-space: pre;"> </span>1.4E-45 / 3.4028235E38</div>
<div style="text-align: justify;">
double<span class="Apple-tab-span" style="white-space: pre;"> </span>64<span class="Apple-tab-span" style="white-space: pre;"> </span>0.0d<span class="Apple-tab-span" style="white-space: pre;"> </span>1.1, 1.2, 1.3<span class="Apple-tab-span" style="white-space: pre;"> </span>4.9E-324 / 1.7976931348623157E308</div>
<div style="text-align: justify;">
char<span class="Apple-tab-span" style="white-space: pre;"> </span>16<span class="Apple-tab-span" style="white-space: pre;"> </span>'\u0000'<span class="Apple-tab-span" style="white-space: pre;"> </span>'a', 'b', 'c'<span class="Apple-tab-span" style="white-space: pre;"> </span>'\u0000'(0) / '\uffff'(65,535)</div>
<div style="text-align: justify;">
String<span class="Apple-tab-span" style="white-space: pre;"> </span>-<span class="Apple-tab-span" style="white-space: pre;"> </span>null<span class="Apple-tab-span" style="white-space: pre;"> </span>"Hello"<span class="Apple-tab-span" style="white-space: pre;"> </span>-</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Furthermore there are the arrays. The array can be made by any type of the previous one, and they can also be made by objects. The arrays can be of one or more dimensions. The arrays must be instancied, and have a static size, defined when their are instancied.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Example of arrays:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
byte[] a = new byte[10];<span class="Apple-tab-span" style="white-space: pre;"> </span>//size of array = 10<br />
int[][] b = new byte[5][5];<span class="Apple-tab-span" style="white-space: pre;"> </span>//array of two dimensions, each one with a size of 5</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
When assiging a value to non-floating numeric variables, it can be assigned from multiples bases: 2(binary), 10(decimal), 16(hexadecimal).</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Examples:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
int a = 0b010101;<span class="Apple-tab-span" style="white-space: pre;"> </span>//binary format. Atention, only for JDK 1.7 or grather<br />
int b = 1234;<span class="Apple-tab-span" style="white-space: pre;"> </span>//decimal format<br />
int c = 0x1234abcd;<span class="Apple-tab-span" style="white-space: pre;"> </span>//hexadecimal format</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>Sergio Lemahttp://www.blogger.com/profile/08414888476925050297noreply@blogger.com0tag:blogger.com,1999:blog-17976919211510869.post-5494193900180178072012-06-10T10:54:00.000-07:002012-06-18T13:05:38.573-07:00Syntaxis<br />
<div style="text-align: justify;">
In this lesson we are going to learn about the syntaxis of the Java programming code. The Java code has classes, inside the classes are the methods, and inside the methods are the sentences. The sentences can be made of three basic types: declaration, conditions and actions. In the declaration we have to create the variables; the conditions decide where way of execution of the code; and the actions are were we act with the variables.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
The declaration are written in the following way: first must be the data type, then the name of the variable, and at the end the semi colon. There are two kinds of declaration: for the primitive data types, and for the rest. For the primitive data types the declaration is easy, we only have to assigned the name of the variable and optionally the value. There can be multiple declaration of variables for a same data type if they are followed by comas.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Examle:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
double a;<br />
int b = 3;<br />
byte c, d;</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
For the rest of the data types (arrays, objects...), the variable must be instancied. What does that mean? That the variable needs a constructor to be initialized. The value of a variable which is not instancied is null. To instancied a variable we will use the operator "new" followed by the constructor. Depending on the construtor, they can requiere some arguments. Here is an example:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
Object a = new Object();<br />
byte[] b = new byte[4];</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
There are some objects that can/must be instancied by a subclass because the first one is only an interface. Here is an example:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
Calendar cal = new GregorianCalendar();</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
The conditions are the one which decides where the code will be executed, and in some cases for assigning a value to a variable. They are always inside parentheses, and return a boolean value. They can be in do-while/while loops, for loops, if conditions, and asigning a value to a boolean variable. Here are some examples:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
int a = 3, b = 4;<br />
if(a == b)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("a is equal to b");</blockquote>
</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
boolean c = (a > b);<span class="Apple-tab-span" style="white-space: pre;"> </span>//c = false</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Finally there are the actions. The actions are where the values of the variables are setted or getted, where the mathematical operations will be done... Those instructions must always finish by a semi colon. Here is an example:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
int a = 3, b = 4;<br />
a = a + 4;<br />
System.out.println("a is equal to " + a);</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Now we are going to talk about the admissible characters. There are the mathematical signs (+, -, *, /, %, =), the logial ones (>, <, ==, !=) and for the commentaries (//, /* */). Then, there are the brackets [] , braces {}, parentheses () and angle brackets <>. The brackets are used in the arrays, to define the index of the array. The braces are used to defined a block of code. This code can own to a class, method or loop. But you can also write some code inside those brackets without any reason. This is made to conserve internal variables only in this block of code. Here is an example:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
int a = 3;<br />
{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>int b = 2;<span class="Apple-tab-span" style="white-space: pre;"> </span>//this variable is exclusive of this block<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>a = a + b;<br />
}<br />
int b = 5;<span class="Apple-tab-span" style="white-space: pre;"> </span>//this variable is not the same as the previous one<br />
a = a + b;</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
And the parentheses are for the methods. There is were we can pass the arguments to the methods. The angle brackets are used to specified a data type in a collection. I will dedicate another article for those data types.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Anothers characters used in the Java code are the following ', ", \, \r, \n... Those are characters used for the texts. The ' is used for determine a signel character. Only one character can be between two '. The " define a string of characters, you can put all the characters inside. And then, there are the escape character and the special ones. The special characters (like \r: carriage return, \n: line return...) are used to make a special behaviour to the text where it is placed. And the escape character is used to print a special character in a text. The special character will never be showed in a text, they will do their own behaviour, and the escape character say that the character following this will be printed in the text. Here is an example:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
char a = 'a';<br />
String b = "Hello";<br />
b = b + '\n';<br />
System.out.println(b);<br />
b = b + "this character is printed \\n";<br />
System.out.println(b);</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
And this is the console output;</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
Hello </blockquote>
<blockquote class="tr_bq">
Hello<br />
this character is printed \n</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>Sergio Lemahttp://www.blogger.com/profile/08414888476925050297noreply@blogger.com0tag:blogger.com,1999:blog-17976919211510869.post-57668694009271538302012-06-10T10:52:00.000-07:002012-06-10T11:22:18.282-07:00Hello World<br />
<div style="text-align: justify;">
The first step is to create a "Hello World". Here we will start with a main programm which only print the message "Hello World" in the console.</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
<span style="background-color: white;">public class HelloWorld{</span><span style="background-color: white;"><span class="Apple-tab-span" style="white-space: pre;"> </span>public static void main(String[] args){</span><span style="background-color: white;"><span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Hello World");</span><span style="background-color: white;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</span><span style="background-color: white;">}</span></blockquote>
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
This code must be saved in a file with the same name as the class (in this case "HelloWorld.java"). Then we can run the example with the following commands:</div>
<div style="text-align: justify;">
<blockquote class="tr_bq">
> java HelloWorld.java</blockquote>
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
For running this example a Java Virtual Machine must be installed in the computer.</div>
<div style="text-align: justify;">
<br /></div>Sergio Lemahttp://www.blogger.com/profile/08414888476925050297noreply@blogger.com0tag:blogger.com,1999:blog-17976919211510869.post-73444317736884582962012-06-10T10:49:00.000-07:002012-06-10T10:49:51.042-07:00Introduction<br />
<div style="text-align: justify;">
The java code programming is used in many devices. This language is very important because is it object oriented, multi-platform and it can be also used for graphic programming. The only thing you need to start programming is to have a Java Virtual Machine installed in the computer/device where you want to make a programm.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
The Java language can be written in simple file, and then compiled with the Java compiler. There are many ways to compile the code: by console or by some IDE (Integrated Development Environment, a programming tool). The most easiest way to programm in any language is to use an IDE. Those tools help us to avoid programming mistakes, import mistakes...</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
The Java language can interact with many external devices, but for this cases, you must import the specific libraries. Those libraries must be correctly imported, otherwise the programm will not run properly. </div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
The worst thing of the Java language, from my side, is the difficulty for working with some utilities of the operating system, like files, serial ports, mouses, keybords... Apart from this, the Java language is one of the most used languages, it can used for a lot of utilities and have a lot of frameworks for the web programming.</div>Sergio Lemahttp://www.blogger.com/profile/08414888476925050297noreply@blogger.com0