Difference between revisions of "W7003 The Basics"

From Coder Merlin
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
== First Steps ==
== First Steps ==
* Emacs and Java
=== Prepare a Directory ===
* mkdir ~/AP-Practice
{{ConsoleLines|
* Within this directory, mkdir Hello, World
{{Lime|john-williams@codermerlin}}:{{Cyan|~/}}$ cd<br/>
* Create a new file, HelloWorld.java
{{Lime|john-williams@codermerlin}}:{{Cyan|~/}}$ mkdir AP-Practice<br/>
** Names matter - the public class name MUST match the file name
{{Lime|john-williams@codermerlin}}:{{Cyan|~/}}$ cd AP-Practice<br/>
* Compile with '''javac'''
{{Lime|john-williams@codermerlin}}:{{Cyan|~/AP-Practice}}$ mkdir HelloWorld<br/>
* An executable must have an entry point which is a '''public''', '''static''' function named ''main'', accepting a single argument, an array of strings.
{{Lime|john-williams@codermerlin}}:{{Cyan|~/AP-Practice}}$ cd HelloWorld<br/>
* Execute with '''java'''
{{Lime|john-williams@codermerlin}}:{{Cyan|~/AP-Practice/HelloWorld}}$ <br/>
}}
=== Create a New File ===
{{ConsoleLines|
{{Lime|john-williams@codermerlin}}:{{Cyan|~/AP-Practice/HelloWorld}}$ emacs HelloWorld.java<br/>
}}
 
Enter the following text:
<syntaxhighlight lang="java">
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
</syntaxhighlight>
 
{{Hint|
* '''Names matter''' - The file name MUST match the name of the public class
* An executable must have an ''entry point'' which is a '''public''', '''static''' function named ''main'', accepting a single argument, an array of strings.
}}
 
=== Compilation ===
A Java program can be compiled with the '''javac''' command.  Note that the argument to the command is the file to be compiled, including the ".java" suffix.  This will produce a file with the ".class" suffix.
{{ConsoleLines|
{{Lime|john-williams@codermerlin}}:{{Cyan|~/AP-Practice/HelloWorld}}$ javac HelloWorld.java<br/>
}}
 
=== Execution ===
A Java program may be executed with the '''java''' command.  Note that the argument to the command is the file to be executed, excluding the ".class" suffix.
{{ConsoleLines|
{{Lime|john-williams@codermerlin}}:{{Cyan|~/AP-Practice/HelloWorld}}$ java HelloWorld<br/>
}}


== Short Cuts ==
== Short Cuts ==
* Compilation and Execution can be combined
We can combine these two commands in bash as follows:
* All of this can occur within emacs
{{ConsoleLines|
* Emacs shortcut and up arrow
{{Lime|john-williams@codermerlin}}:{{Cyan|~/AP-Practice/HelloWorld}}$ javac HelloWorld.java && java HelloWorld<br/>
* Define keyboard macro with f3 and f4, execute with f4
}}
** CTRL-X, CTRL-S, ALT-SHIFT-&, Up-Arrow, ENTER
 
We can perform the same actions in emacs using the '''Async Shell Command''' and typing in the same line as we used in bash.  The Async Shell Command can be invoked in emacs as {{VerySpecialKey|ALT|SHIFT|&}}.  Then, type: <code>javac HelloWorld.java && java HelloWorld</code> {{Key|ENTER}}.
 
In order to repeat the command in emacs, again enter the key combination {{VerySpecialKey|ALT|SHIFT|&}} followed by {{Key|↑}} {{Key|ENTER}}.
 
=== Macros ===
While we can enter this command very quickly, we can do even better by defining a ''macro'' within emacs.  Our macro will first save the file to preserve our changes, and then compile and execute the program.  We beginning defining a macro with {{Key|F3}} and complete the definition with {{Key|F4}}.  We can then execute the macro with {{Key|F4}}.
 
Thus, the complete sequence for defining the macro is:<br/>
{{Key|F3}} {{SpecialKey|CONTROL|X}} {{SpecialKey|CONTROL|S}} {{VerySpecialKey|ALT|SHIFT|&}} {{Key|↑}} {{Key|ENTER}} {{Key|F4}}


== Semicolons ==
We can now execute the macro by simply pressing {{Key|F4}}.
 
== Language Basics ==
=== Semicolons ===
Semicolons are ''required'' at the end of each statement
Semicolons are ''required'' at the end of each statement


== Static Typing ==
=== Static Typing ===
* Java is statically typed
* Java is statically typed
* Variables must be declared before they can be used, including the type
* Variables must be declared before they can be used, including the type
* Types define the possible values of the variable and the operations that may be performed upon it
* Types define the possible values of the variable and the operations that may be performed upon it
* Values do not always have to be assigned when a variable is declared, though it is best practice to do so.  In some cases, a reasonable default value will be assigned by the compiler. 


== Primitive Types ==
=== Primitive Types ===
NOTE:  All primitive types begin with a lowercase letter
NOTE:  All primitive types begin with a lowercase letter


Line 36: Line 80:
* double
* double


== Common Errors ==
=== Common Errors ===
<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
int gpa;
int gpa;
Line 47: Line 91:
</syntaxhighlight>
</syntaxhighlight>


== New ==
=== Implicit Casting ===
* Magnitude of numeric types is preserved (precision may be lost)
 
=== Explicit Casting ===
* Required when magnitude of numeric type may not be preserved
 
=== Division by Zero ===
* Compare and contrast integer vs floating point
 
=== New ===
The '''new''' keyword is not used for primitive types
The '''new''' keyword is not used for primitive types


== Primitive numeric types ==
=== Primitive numeric types ===
* Primitive numeric types overflow and underflow ''silently''
* Primitive numeric types overflow and underflow ''silently''

Latest revision as of 23:46, 27 November 2019

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

First Steps[edit]

Prepare a Directory[edit]

john-williams@codermerlin:~/$ cd

john-williams@codermerlin:~/$ mkdir AP-Practice

john-williams@codermerlin:~/$ cd AP-Practice

john-williams@codermerlin:~/AP-Practice$ mkdir HelloWorld

john-williams@codermerlin:~/AP-Practice$ cd HelloWorld

john-williams@codermerlin:~/AP-Practice/HelloWorld$

Create a New File[edit]

john-williams@codermerlin:~/AP-Practice/HelloWorld$ emacs HelloWorld.java

Enter the following text:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
Hint.pngHelpful Hint
  • Names matter - The file name MUST match the name of the public class
  • An executable must have an entry point which is a public, static function named main, accepting a single argument, an array of strings.

Compilation[edit]

A Java program can be compiled with the javac command. Note that the argument to the command is the file to be compiled, including the ".java" suffix. This will produce a file with the ".class" suffix.

john-williams@codermerlin:~/AP-Practice/HelloWorld$ javac HelloWorld.java

Execution[edit]

A Java program may be executed with the java command. Note that the argument to the command is the file to be executed, excluding the ".class" suffix.

john-williams@codermerlin:~/AP-Practice/HelloWorld$ java HelloWorld

Short Cuts[edit]

We can combine these two commands in bash as follows:

john-williams@codermerlin:~/AP-Practice/HelloWorld$ javac HelloWorld.java && java HelloWorld

We can perform the same actions in emacs using the Async Shell Command and typing in the same line as we used in bash. The Async Shell Command can be invoked in emacs as ALT-SHIFT-&. Then, type: javac HelloWorld.java && java HelloWorld ENTER.

In order to repeat the command in emacs, again enter the key combination ALT-SHIFT-& followed by ENTER.

Macros[edit]

While we can enter this command very quickly, we can do even better by defining a macro within emacs. Our macro will first save the file to preserve our changes, and then compile and execute the program. We beginning defining a macro with F3 and complete the definition with F4. We can then execute the macro with F4.

Thus, the complete sequence for defining the macro is:
F3 CONTROL-X CONTROL-S ALT-SHIFT-& ENTER F4

We can now execute the macro by simply pressing F4.

Language Basics[edit]

Semicolons[edit]

Semicolons are required at the end of each statement

Static Typing[edit]

  • Java is statically typed
  • Variables must be declared before they can be used, including the type
  • Types define the possible values of the variable and the operations that may be performed upon it
  • Values do not always have to be assigned when a variable is declared, though it is best practice to do so. In some cases, a reasonable default value will be assigned by the compiler.

Primitive Types[edit]

NOTE: All primitive types begin with a lowercase letter

  • byte (8-bit)
  • short (16-bit)
  • int (32-bit)
  • long (64-bit)
  • boolean
  • char (16-bit unicode)
  • float
  • double

Common Errors[edit]

int gpa;
gpa = 3.75;

float f = 10.5; 

byte b = -128;
b -= 1;

Implicit Casting[edit]

  • Magnitude of numeric types is preserved (precision may be lost)

Explicit Casting[edit]

  • Required when magnitude of numeric type may not be preserved

Division by Zero[edit]

  • Compare and contrast integer vs floating point

New[edit]

The new keyword is not used for primitive types

Primitive numeric types[edit]

  • Primitive numeric types overflow and underflow silently