{"id":3152,"date":"2020-06-30T09:43:39","date_gmt":"2020-06-30T16:43:39","guid":{"rendered":"http:\/\/firstinmichigan.us\/FTC\/?page_id=3152"},"modified":"2022-10-18T06:35:50","modified_gmt":"2022-10-18T13:35:50","slug":"java-syntax-basics","status":"publish","type":"page","link":"https:\/\/firstinmichigan.us\/FTC\/java-syntax-basics\/","title":{"rendered":"Java Syntax Basics"},"content":{"rendered":"\n<script src=\"https:\/\/cdn.jsdelivr.net\/gh\/google\/code-prettify@master\/loader\/run_prettify.js\"><\/script>\n<script id=\"MathJax-script\" async=\"\" src=\"https:\/\/cdn.jsdelivr.net\/npm\/mathjax@3\/es5\/tex-mml-chtml.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Note About This Guide<\/h2>\n\n\n\n<p>If you are new to Java programming, it is expected that you may not understand everything at first. You will still be able to read and understand the next guide on <a href=\"http:\/\/firstinmichigan.us\/FTC\/ftc-java-basics\/\">FTC Java Basics<\/a>, and you can use this guide as a reference whenever you need.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Java Syntax Basics<\/h2>\n\n\n\n<p>Java is a line based language, meaning that the computer will interpret your program as typed lines of code. As a result, the way in which you write your code must follow a strict structure, known as the <strong>syntax<\/strong>. Here is the most common syntax you will use in Java.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Comments<\/h3>\n\n\n\n<p>Sometimes&nbsp;you will want to leave a note in your code (a reminder, an explanation, etc) for yourself. This can be done through <strong>comments<\/strong>. Any line that begins with the characters <code>\/\/<\/code> will be treated as a comment, meaning that your program will simply ignore it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted prettyprint\">\/\/ This is an example comment. Use comments to describe what your code does or to remind yourself of something.\n<\/pre>\n\n\n\n<p>You can also make multi-line comments by beginning with <code>\/*<\/code> and ending with <code>*\/<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted prettyprint\">\/*\nThis is part of the comment\nand this is also part of the comment\n*\/\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Classes<\/h3>\n\n\n\n<p>In Java, a class is a program that contains <strong>variables<\/strong> (values that the program stores) and <strong>methods<\/strong> (functions that define the behavior of the program). A simple class looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted prettyprint\">public class MyClass extends ParentClass {\n    \/\/ Code for this class' variables and methods will go here\n}\n<\/pre>\n\n\n\n<p>The syntax of a basic class is <code>access-level class ClassName extends ParentClassName {}<\/code>.<\/p>\n\n\n\n<p>The <code>access-level<\/code> simply defines where this class can be used. For classes, we usually use <code>public<\/code>, which means that this class can be used by anything. If the concept of <strong>access levels<\/strong> is confusing, that&#8217;s okay; for FTC programming, they are not too important to understand.<\/p>\n\n\n\n<p>The <code>extends ParentClassName<\/code> part of the syntax is optional. If included, this will cause the class to <strong>inherit<\/strong> the variables and methods of another &#8220;parent&#8221; class.<\/p>\n\n\n\n<p>The brackets (<code>{}<\/code>) is the class&#8217;s block, which will contain all of the code that we will write in the class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Data Types<\/h3>\n\n\n\n<p>Before discussing methods and variables, it is important to discuss <strong>data types<\/strong>. All variables and values returned by methods have a type that defines what type of data it is. These are a few of the most basic (<strong>primitive<\/strong>) data types in Java:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Type<\/th><th>Description<\/th><th>Example Values<\/th><\/tr><\/thead><tbody><tr><td><code>int<\/code><\/td><td>A whole number (integer)<\/td><td><code>4<\/code>, <code>-7<\/code> <\/td><\/tr><tr><td><code>double<\/code><\/td><td>A decimal number<\/td><td><code>1<\/code>, <code>7.2<\/code>, <code>-0.3<\/code><\/td><\/tr><tr><td><code>boolean<\/code><\/td><td>A &#8220;on or off&#8221;, &#8220;yes or no&#8221;, &#8220;true or false&#8221; value.<\/td><td><code>true<\/code>, <code>false<\/code> (these are the <em>only<\/em> possible values)<\/td><\/tr><tr><td><code>string<\/code><\/td><td>A &#8220;string&#8221; of characters (basically any text).<\/td><td><code>\"Hello world!\"<\/code>, <code>\"left_drive\"<\/code><br>(string values must always be surrounded by quotes to differentiate them from regular code)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Notice that all of the primitive data types start with a lower case letter (like <code>int<\/code>). There are also <strong>object data types<\/strong>, which are based off of classes and start with an upper case letter. For instance, in FTC, there is a <code>DcMotor<\/code> object data type based on the <code>DcMotor<\/code> class; as the name implies, variables with this data type contains the variables and methods for a motor on the robot.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Methods<\/h3>\n\n\n\n<p>Methods define the various functions of a class. Within the class&#8217;s block (brackets), we can define a method like so: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted prettyprint\">public class MyClass extends ParentClass {\n    public void myMethod() {\n        \/\/ Method code will go here\n    }\n}\n<\/pre>\n\n\n\n<p>The syntax of a basic method is <code>access-level return-type methodName() {}<\/code>.<\/p>\n\n\n\n<p>The <code>access-level<\/code> works the same as with classes: it defines what can use the method. Here we just use <code>public<\/code>.<\/p>\n\n\n\n<p> The <code>return-type<\/code> is the data type (<code>int<\/code>, <code>DcMotor<\/code>, etc) of the value that this method <strong>returns<\/strong> as output when ran. If the method does not return any value, the return type is simply <code>void<\/code>. <\/p>\n\n\n\n<p>The <code>()<\/code> contains any method <strong>parameters<\/strong> (inputs). We will talk about those more later in the guide.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Statements<\/h3>\n\n\n\n<p>We have discussed the syntax structures in Java that will contain our code (classes and methods), but how do we actually write code to put into those structures? In Java, <strong>statements<\/strong> are lines of code that can perform various actions when ran, such as <strong>defining variables<\/strong>, <strong>assigning (setting) variables<\/strong>, and <strong>invoking (running) class methods<\/strong>.<\/p>\n\n\n\n<p>Statements in Java must always end with a semicolon (<code>;<\/code>) character; otherwise, the computer will not know where the statement ends. Here are some common types of statements:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Variable Definition<\/h4>\n\n\n\n<p>We can use statements to define (create) new variables to store values within our code. Variables can be defined either in the class (called &#8220;<strong>instance variables<\/strong>&#8220;) or in a method (called &#8220;<strong>local variables<\/strong>&#8220;). Here are examples of both in use:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted prettyprint\">public class MyClass extends ParentClass {\n    \/\/ Instance variable definition\n    private DcMotor myMotor;\n\n    public void myMethod() {\n        \/\/ Local variable definition\n        double myDouble = 1;\n    }\n}\n<\/pre>\n\n\n\n<p>The <strong>instance variable<\/strong> statement is inside of the class, but not the method. The syntax is <code>access-level type variableName;<\/code>.<\/p>\n\n\n\n<p>We usually make our class variables <code>private<\/code> (however, if you make the variables <code>public<\/code> or don&#8217;t even include the access level, it will still work as intended for FTC programming purposes).<\/p>\n\n\n\n<p>The <strong>local variable<\/strong> syntax is similar to the instance variable syntax, except there is no access-level included since local variables can <em>only<\/em> be used within the method that they are defined in.<\/p>\n\n\n\n<p>You can also set the initial value of a variable by including <code>= value<\/code> at the end of the definition (but before the semicolon, of course).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Variable Assignment<\/h4>\n\n\n\n<p>We can write statements within our methods to set the values of defined variables. For instance:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted prettyprint\">public class MyClass extends ParentClass {\n    int exampleInt;\n\n    public void myMethod() {\n        \/\/ Variable assignment\n        exampleInt = 7;\n    }\n}\n<\/pre>\n\n\n\n<p>Since the variable is already defined, you do <em>not<\/em> include the variable&#8217;s data type in its assignment statement.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Method Invocation<\/h4>\n\n\n\n<p>Statements can also be used to invoke (run) a method. You can either invoke  another method in your class or invoke a method contained in an object variable. To invoke a method in an object variable, you have to use a <code>.<\/code> operator to access it. For instance, to invoke a method named <code>myMethod<\/code> contained in a variable named <code>myObject<\/code>, you would write:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted prettyprint\">myObject.myMethod();\n<\/pre>\n\n\n\n<p>If a method requires parameters (inputs), you must list your input values (or variables and expressions) inside of the method&#8217;s parenthesis, separated by commas. For instance:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted prettyprint\">\/\/ Let's say myMethod is defined like this in our class:\npublic void myMethod(String caption, int value) {\n    \/\/ Code using the caption and value input variables\n}\n\/\/ The method takes two inputs: a String and an int.\n\/\/ Thus, when invoking the method, we have to pass those inputs like so:\nmyMethod(\"Example Name\", 7);\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Expressions<\/h3>\n\n\n\n<p>In our examples for the different types of statements, we simply used raw values (like <code>7<\/code>) for our variable definitions\/assignments and method invocations. However, you can also use more complicated <strong>expressions<\/strong> in your statements, comprised of values, variables, math, conditionals, and other expressions.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Arithmetic Expressions<\/h4>\n\n\n\n<p>An arithmetic expression use mathematics to return a calculated value. Here is an example of defining a variable named z using an arithmetic expression.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted prettyprint\">int x = 3;\nint y = 2;\nint z = (x + y) * 4;\n<\/pre>\n\n\n\n<p>As expected, the value of <code>z<\/code> will end up being \\( 20 \\), the result of  \\(  (3 + 2) \\times 4 \\).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Boolean Expressions<\/h4>\n\n\n\n<p>A boolean expressions returns a boolean value (true or false). Boolean expressions can contain <strong>comparison operators<\/strong> that compare two values. Here is an example of a comparison:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted prettyprint\">int x = 3;\nint y = 2;\nboolean greater = x > y;\n<\/pre>\n\n\n\n<p>The <code>><\/code> comparison determines if one value is greater than another, and returns <code>true<\/code> if it is greater or <code>false<\/code> otherwise. In the above example, the value of <code>greater<\/code> will be <code>true<\/code> since the value of <code>x<\/code> (3) is greater than <code>y<\/code> (2). <\/p>\n\n\n\n<p>Here is list of all comparisons:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Operator<\/th><th>Name<\/th><th><strong>Example<\/strong>s that Return <strong><code>true<\/code><\/strong><\/th><\/tr><\/thead><tbody><tr><td>==<\/td><td>Equal<\/td><td><code>2 == 2<\/code>, <code>\"word\" == \"word\"<\/code><\/td><\/tr><tr><td>!=<\/td><td>Not equal<\/td><td><code>0 != 1<\/code>, <code>\"word\" != \"other_word\"<\/code><\/td><\/tr><tr><td>><\/td><td>Greater than<\/td><td><code>3 > 2<\/code><\/td><\/tr><tr><td>&lt;<\/td><td>Less than<\/td><td><code>2 &lt; 3<\/code><\/td><\/tr><tr><td>>=<\/td><td>Greater than or equal to<\/td><td><code>2 >= 2<\/code>, <code>3 >= 2<\/code><\/td><\/tr><tr><td>&lt;=<\/td><td>Less than or equal to<\/td><td><code>2 &lt;= 2<\/code>, <code>2 &lt;= 3<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Boolean expressions can also contain <strong>logical operators<\/strong> which compare two boolean values. A common operator is <strong>and<\/strong> (<code>&amp;&amp;)<\/code>, which returns <code>true<\/code> if both values are <code>true<\/code> and <code>false<\/code> otherwise. For instance:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted prettyprint\">int x = 3;\nint y = 2;\nboolean andResult = (x > y) && (x == 2);\n<\/pre>\n\n\n\n<p>The boolean expressions on the left <strong>and<\/strong> the right of the <strong>&amp;&amp;<\/strong> operator are both <code>true<\/code>, so the value of <code>andResult<\/code> will be set to <code>true<\/code>. Here is an example where <code>andResult<\/code> is <code>false<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted prettyprint\">int x = 7;\nint y = 2;\nboolean andResult = (x > y) && (x == 2);\n<\/pre>\n\n\n\n<p>In this case the first expression (<code>x > y<\/code>) will still be <code>true<\/code>, but the second expression (<code>x == 2<\/code>) will now be <code>false<\/code>. Thus, <code>andResult<\/code> will be <code>false<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Further Learning<\/h2>\n\n\n\n<p>If you want to learn more about the Java programming language <strong>(not specific to FTC programming, only Java in general)<\/strong> beyond the information in this concise guide, <a rel=\"noreferrer noopener\" href=\"https:\/\/www.codecademy.com\/learn\/learn-java\" target=\"_blank\">Codecademy&#8217;s free Java course<\/a> is a good place to start; lessons 1, 2, 3, 4, and 6 of the course are most relevant to the Java skills used in FTC programming. Note that in depth Java knowledge is not required in order to understand the next guides, but extra knowledge can definitely be beneficial.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Note About This Guide If you are new to Java programming, it is expected that you may not understand everything at first. You will still be able to read and understand the next guide on FTC Java Basics, and you can use this guide as a reference whenever you need. Java Syntax Basics Java is a line based language, meaning that the computer will interpret your program as typed lines of code. As a result, the way in which you write your code must follow a strict structure, known as the syntax. Here is the most common syntax you will use in Java. Comments Sometimes&nbsp;you<a class=\"more-link\" href=\"https:\/\/firstinmichigan.us\/FTC\/java-syntax-basics\/\">Read More &rarr;<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"nf_dc_page":"","footnotes":""},"class_list":["entry","page","publish","author-eschnabel","post-3152"],"_links":{"self":[{"href":"https:\/\/firstinmichigan.us\/FTC\/wp-json\/wp\/v2\/pages\/3152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/firstinmichigan.us\/FTC\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/firstinmichigan.us\/FTC\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/firstinmichigan.us\/FTC\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/firstinmichigan.us\/FTC\/wp-json\/wp\/v2\/comments?post=3152"}],"version-history":[{"count":59,"href":"https:\/\/firstinmichigan.us\/FTC\/wp-json\/wp\/v2\/pages\/3152\/revisions"}],"predecessor-version":[{"id":3301,"href":"https:\/\/firstinmichigan.us\/FTC\/wp-json\/wp\/v2\/pages\/3152\/revisions\/3301"}],"wp:attachment":[{"href":"https:\/\/firstinmichigan.us\/FTC\/wp-json\/wp\/v2\/media?parent=3152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}