<< Back Computer Science Club

September 25 2019 - Java 1 (setup, data types)

This was our first Computer Science Club meeting (very cool). Here's pretty much what we did (sorry this might be tedious to read but like just show up to the meetings and you won't have to).




Using Eclipse and Java

About

Eclipse is an IDE (integrated development environment) that allows us to code and compile with ease. We're able to handle our projects more efficiently and code with the help of content assist (a heck load of more stuff but for now we don't need to use it all).


Projects

Create a project by right clicking on the 'Projects' view on the left -> New -> Java Project
Enter a name that you'd want to use and hit [Enter]. This name is only for your reference and won't be in any part of your actual program.


Packages

Packages are like folders (for what we're doing right now). Create a package by right clicking on 'src' within your project -> New -> Package
Naming packages usually follow certain naming conventions. They look like a URL backwards, followed by a project identifier, followed by an optional category.
(example: com.google.myproject and com.google.myproject.utilities)


Classes

Classes are where all of your code is run from. We'll get into more details later, but for now, you just need a few to run basic code.
Classes follow naming conventions as well: they must start with a CAPITAL LETTER and have no spaces.
(example: HelloWorld)




What is Java?

It's an object-oriented programming language (good for basics but also very useful in the future). Like literally pretty much everything runs Java (television, computer, etc.)


Important things to know

- All lines must end with a semicolon ; (except for methods or other containers that use { })
- Everything is cAse SenSitIvE
- Spacing in between lines doesn't matter (so space out code for easier reading)




The Main Method

Your Java program needs to have a main method. We'll go into detail about what each of the modifiers mean, but for now, you'll just need to copy exactly as shown below:

public static void main(String[] args){

}


This is where your code will start running from when the program is run. It'll start with the first line of this method and continue by reading from left to right top to bottom.




Data Types and Variables

About Data Types

There are a lot of different variable types you can create. You store values and content in variables, by the way. See below for a bunch of the common ones.


Initializing them with code

To create a variable, you'd type it out like so:

String hello = "Hello world!";

Where "String" is the data type (see below), "hello" is the identifier of the variable (this can be whatever you want, everytime you use this variable in your code, you'll refer to it with this name), "=" means your assigning the value on the right to the variable on the left, and "Hello world!" is replaced with an appropriate value.



String
A String is any piece of text (can include letters, numbers, symbols, spaces, etc.).
Must be surrounded by double quotation marks "

String myAddress = "1 Apple Park Way";


int
An int is an integer (any full number). It can be negative but no decimals.

int numberOfEmployees = 12000;


boolean
A boolean can only be true or false.

boolean isCool = true;


double
A double is any number including a decimal point. If you set it to a whole number, it will automatically include the .0 when used.

double revenueInBillions = 258.5;


char
A char is a character (only one letter, number, symbol, etc.).
Must be surrounded by single quotation marks '

char firstLetter = 'A';

Printing to the Console

You can print to the console by using the following line of code:
System.out.println(<variable>);

Replace <variable> with either the variable identifier to print out or any value.




If you have any questions, ask them at Computer Science Club!