The Geeky Way - J4A (Java for Android), Programming//thegeekyway.com/2014-02-22T23:58:00+05:30Creating your first Java Program (J4A - 2)2014-02-22T23:58:00+05:302014-02-22T23:58:00+05:30gagan93tag:thegeekyway.com,2014-02-22:/creating-first-java-program/<p><u>Beginning Java Programming</u></p>
<p>Some institutes/tutorials start java directly using Netbeans or Eclipse IDE. But this is not the correct way. A better method is to use a basic text editor like Notepad2 or Notepad++ to begin Java. IDE’s provide shortcuts which experienced programmers use. If beginners opt for …</p><p><u>Beginning Java Programming</u></p>
<p>Some institutes/tutorials start java directly using Netbeans or Eclipse IDE. But this is not the correct way. A better method is to use a basic text editor like Notepad2 or Notepad++ to begin Java. IDE’s provide shortcuts which experienced programmers use. If beginners opt for these methods, they will leave half of the work for IDE and miss this ‘half’.</p>
<p><strong>IDE</strong> is an Integrated Development Environment or a software which accelerates software development and provides features like syntax completion, error detection,error correction, auto indentation, drag and drop for G.U.I. and much more. One should go for an IDE only after practising a programming language on text editors.</p>
<p>The best text editor for starting java is TextPad (<a href="http://www.textpad.com/download/index.html" title="link">download from here</a>). The best part of TextPad is that it provides Program compilation and execution shortcuts just like Turbo C++. ‘Ctrl + 1’ can be used to compile a Java program and ‘Ctrl + 2’ can be used to execute a compiled program.</p>
<p><u>First Simple Java Program</u></p>
<p><strong><u> </u></strong></p>
<p><strong>class Demo</strong></p>
<p><strong>{</strong></p>
<p><strong> public static void main(String[] args)</strong></p>
<p><strong> {</strong></p>
<p><strong> System.out.println(“My first java program”);</strong></p>
<p><strong> }</strong></p>
<p><strong>}</strong></p>
<p><u>Noteworthy points about this small program</u> :</p>
<ol>
<li>In Java, even the main method lies inside a class, and Java file is saved with the name of the class containing main method so, the above code should be saved as “Demo.java”.</li>
<li>In Java, ‘functions’ are known as ‘methods’ and variables are called as ‘attributes’</li>
<li>Indentation is used to increase readability. Programmers <strong>must</strong> use indentation to make their code look clean.</li>
<li>Main method is both public and static. Public methods are accessible outside the class also (public is similar to what it was in C++). As main method is accessed by Java Virtual Machine during execution, so it <strong>must</strong> be public. Static keyword is described later. For now, just keep in mind that ‘static’ method or variable has only copy for entire class irrespective of no. of objects.</li>
<li>The main method takes a string array as an argument. This argument can be passed when executing the program.(Consider String to be a data type till further discussion)</li>
<li>System.out.println() is similar to printf or cout\<\<. Here System is a class, out is a stream and println() is a method. ‘out’ is a <u>static stream </u>so it is accessed by name of class (System) rather than object of system class.</li>
</ol>
<p>Write the above program (don’t copy) in a simple text editor (or Textpad). Save the file as “Demo.java” at some specific path. If you are using Textpad, use shortcuts described above to compile and execute the code.</p>
<p>If you are using a simple text editor, follow these steps</p>
<ol>
<li>Open command prompt (goto start -> run -> cmd - > Enter).</li>
<li>Navigate to the directory containing the file which you have just saved.</li>
<li>To compile java program, type “javac Demo.java”. Here ‘javac’ is the java compiler application and Demo.java is the name of the java program which we are compiling. After this step, Demo.class file is created in the directory.</li>
<li>To execute java program, type “java Demo”. Here java is the name of application which will execute the .class file and show the output as shown here.</li>
</ol>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/61.jpg"><img alt="6" src="//thegeekyway.com/uploads/images/2014/02/61.jpg"></a></p>
</div>Moving from C++ to Java (J4A - 1)2014-02-22T23:51:00+05:302014-02-22T23:51:00+05:30gagan93tag:thegeekyway.com,2014-02-22:/moving-c-java/<p><u>C to C++ and C++ to Java:</u></p>
<p>C was a powerful programming language but used structural approach. So when the length of code exceeded a particular no. of lines, the code became unmanageable. So this led to Object Oriented Programming techniques and birth of C++. But C++ compilers at that …</p><p><u>C to C++ and C++ to Java:</u></p>
<p>C was a powerful programming language but used structural approach. So when the length of code exceeded a particular no. of lines, the code became unmanageable. So this led to Object Oriented Programming techniques and birth of C++. But C++ compilers at that time were not platform independent. You needed a different code for Windows, different for Mac, Unix and other operating systems. Today programs written in C++ are also categorized by 32 and 64 bit versions. So C++ followed the W.O.C.A. principle, i.e. <u>Write Once, Compile Anywhere</u>. The main focus was to create programs which could execute on any platform and any O.S. These forces led to the development of Java (originally called as ‘Oak’) which followed W.O.R.A. principle, i.e. <u>Write Once, Run Anywhere.</u></p>
<p><u> </u></p>
<p><u>Where the Magic lies?</u></p>
<p>So what magic is there in Java which was not in older languages? Basically all the older compilers compile the source code (program) to native executable file (like .exe in windows). Here ‘native executable file’ means a file which is created for some target OS and is ready to execute on that OS. So we need to create a different ‘native’ file for different OS and different processor architectures (32 and 64 bit).</p>
<p>But in Java, the code is not directly compiled to native executable. The output of java compiler is a .class file, irrespective of OS / processor architecture. This .class file is a set of optimized instruction for a virtual machine called JVM (Java Virtual Machine). The creators of Java implemented this virtual machine (JVM) for different operating systems. So now the programmers need not worry about the target operating system or processor. They just have to write the code and install JVM on the target system, JVM handles the rest.</p>
<p><u>Understanding Simple Java Terms:</u></p>
<p><u>JVM</u> is simply a virtual machine included in JDK as well as JRE.</p>
<p><u>JDK</u> is Java Development Kit for Java Developers. It includes Java Runtime Environment(JRE) and all other related tools needed by developers.</p>
<p><u>JRE</u> is Java Runtime Environment. It <strong>excludes</strong> the development tools and encapsulates only runtime environment. When you create a java application for a client, just install JRE on client side and your application will execute.</p>
<p>Remember, <u>Java code is platform independent but Java Runtime Environment is specific for each platform.</u></p>
<p>+-----------------------------------------------------------------------+
| <div> |
| |
| <u>The Android Counterpart</u>: |
| Android Devices have DVM (Dalvik Virtual Machine) rather than JVM |
| (Java Virtual Machine). You can think of Dalvik as mobile or android |
| specific implementation of JVM. You need not know about Dalvik for |
| now, for those who are interested can visit |
| <a href="http://stackoverflow.com/questions/3536786/dalvikvm-vs-javavm- |
| in-android">here</a>. |
| |
| </div> |
+-----------------------------------------------------------------------+</p>
<p><u>Downloading & Installing JDK</u></p>
<p>Download JDK from <a href="http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html">here</a>.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/112.jpg"><img alt="1" src="//thegeekyway.com/uploads/images/2014/02/112.jpg"></a></p>
</div>
<p>Scroll down to see screen as shown above. Accept the License Agreement to start download. As you can see, JDK is implemented for almost all major operating systems. After downloading, install JDK with default settings. After installation, go to</p>
<p>C:/program files/java/jdk\<version_name>/bin/</p>
<p>Make sure ‘javac’ and ‘java’ are present in this folder. Now copy the complete path from the address bar of explorer as shown in the pic below.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/25.jpg"><img alt="2" src="//thegeekyway.com/uploads/images/2014/02/25.jpg"></a></p>
</div>
<p>Now, follow these steps</p>
<p>Go to Start Menu -> Run. Type ‘sysdm.cpl’ and press OK. You get the following screen.</p>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/32.jpg"><img alt="3" src="//thegeekyway.com/uploads/images/2014/02/32.jpg"></a> Click on ‘Advanced’ tab and then on Environment Variables button.</p>
</div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/41.jpg"><img alt="4" src="//thegeekyway.com/uploads/images/2014/02/41.jpg"></a> You will get following screen now</p>
</div>
<div style="text-align:center">
<p><a href="//thegeekyway.com/uploads/images/2014/02/51.jpg"><img alt="5" src="//thegeekyway.com/uploads/images/2014/02/51.jpg"></a> </p>
</div>
<p>Create a new user variable using button as shown above. Write the following values in text field provided.</p>
<p>Variable name : path</p>
<p>Variable value : \<text you had copied>.</p>
<p>Press OK to save and close all previous dialog boxes.</p>
<p>Now your Java Environment Variable is set and if you have followed the steps correctly, you will be able to compile and run java applications. To check if you have followed the above procedure correctly, goto start menu -> Run and type</p>
<p>java and press enter, if you get a flashing black screen, type</p>
<p>javac and press enter, if you still get a flashing black screen, it means the variables are correctly setup. If you get some error when you type java or javac, you might have done some mistake in setting up variable. It’s better to restart your computer and try again.</p>
<p>The next tutorial describes creating your first simple Java program.</p>
<p><a href="http://www.thegeekyway.com/creating-first-java-program" title="link">Link to next tutorial</a></p>