Post: Learn Java Basics
03-05-2015, 09:58 PM #1
primetime43
Knowledge is power Tiphat
(adsbygoogle = window.adsbygoogle || []).push({});
Hi there NGU! So today I am going to make this thread to help those who want to further their knowledge in Java. So to start out, I recommend using BlueJ, you can go to my other thread to get BlueJ here: You must login or register to view this content.

So if you clicked on the link to my other thread, you already know how to make a class and print out in the console. So first off we will start with the basic primitive data types of java.
    
[U]Integer Types (from smallest to largest)[/U]
byte
short
int
long
[U]Floating Point Types[/U]
float
double
[U]String Types
[/U]char
String

And then also there is the boolean, not sure what type it is though.

So you want to use an integer type to store a whole number, a floating point type to store a decimal, and use the string types to store a string.


So in your main class, you're gonna want to use this
     public static void main(String[] args) 
{
//code in here
By the way, two slashes like this // is a comment and will not be read as code by your program. You can also do a multi - line comment by doing this

/*
* Comment in here
* etc
*/

}


So inside the main above, you may want to print something out in the console, to do so, you do this:
    
System.out.println(""); //prints out whatever string is inside the "". The println means it will print on that line. Which means it will go to a new line each time
System.out.print(""); //Just using print means it prints out and does not go to a new line



Now somethings you can use also in printing things out are
    
\n - new line
\t - tab
\b - backspace/r - return
\' - Inserts a single quote
\" - Inserts a double quote
\\ - Inserts a backslash


Sample code with using strings etc:
    
public class FunWithString
{
public static void main(String [] args)
{
System.out.println("This is a String"); //String is an object data type

System.out.print("Roses are Red, ");
System.out.println("Violets are Blue, ");
System.out.println("\tbut I have a \"committment\" issues");
System.out.println("so I think we should be friends, \nat this point in our relationship\\journey.");


System.out.println("This is concatenation " + 24 + 45);
System.out.println("This is adding " + (24+45));
int num1 = 10;
System.out.println("The total is: " + num1 + " boxes.");

String s1 = "The total is: ";
String s2 = "Banana";
String s3 = "Apple";
String s4 = "Orange";
String s6 = "Apple";

System.out.println(s2 + s3);
System.out.println(s3.concat(s4));
System.out.println(s4.length());
System.out.println(s4.substring(1,4));
String s5 = s4.substring(1, 2);
System.out.println(s5);
char c1 = s4.charAt(1);
System.out.println(c1);
System.out.println(!(s4.equals(s6)));

s4 = s4.toUpperCase();

s3 = s3.toLowerCase();

}
}


Here is some sample code with A LOT of NOTES in it so make sure you read them! :
    
public class Class1
{//Start class
public static void main (String[] args)
{//Start main
System.out.print("Hellow World!");
System.out.println("Yay!");


//Integer Data Type - Primitive Data type
// 4 integer data types - byte, short, int, long ///////////////////////////////
byte b1; //assigned
b1 = 0; //initialized //256
short s1 = 0; //assigned and initialized
int num1 = 0;
long dig1 = 0;


int num2 = 0, num3 = 4, num4 = 7;
byte b2, b3 = 10, b4 = 1;


int dayOfWeek = 1; //good variable name
int totalPay = 0;
int NUMBER_OF_WIDGETS = 0;


//Floating Point Variables - Primitive Data Types //////////////////// float, double
float f1 = 0.0F;
double d1 = 0.0D;


int iResult = 0;
double dResult = 0.0;


iResult = 4/3;
dResult = 4/3; //assignment conversion
System.out.println(iResult);
System.out.println(dResult);


dResult = 4.0/3; //Arithmetic Promotion


float fResult = (float)4.0/(float)3.0;
iResult = (int)(4.0/3.0); //casting


System.out.println(dResult);


System.out.println(4.0/3.0);
System.out.println(4.0F/3.0F);
num2 = 4;
num2++; //num 2 = num2 + 1;
System.out.println(num2);
System.out.println(++num2);


System.out.println(num2--);
System.out.println(num2);
System.out.println(--num2);


/*Operations on integers: +, -, *, /, %, ++, --, +=, -= , *=, /= */


num2 = 5;
num2*=3; //num2 = num2 * 3;
System.out.println(num2);


num2-=7; //num2 = num2 - 7;
System.out.println(num2);


num2 %= 3;
System.out.println(num2);


float f2 = 0.0F;
f2 = 1.0F;
System.out.println(f2);


//Primitive Data Type: boolean


boolean flag = true;
boolean bool1 = false, boo2;


/*boolean operators: == "equal to"
* != "not equal"
* && "and"
* ! "not"
* || "or"
* > "greater than"
* < "less than"
* <= "less than or equal to"
* >= "greater than or equal to"
*/


//Primitive Data type: character
char c1 = 'a';
char c2 = 'A',c3,c4='#';


//Operations with characters: <, >, <=, >=, =, !=


System.out.println('a'=='A'Winky Winky;
}//End main
}//End class
//End file


So if you want to ever have some user input, you can use a scanner. Now to use scanner you have to import it and declare it. Also ill show how to use the formatter.
    
import java.util.Scanner;//imports the scanner class
import java.text.DecimalFormat;//imports the decimal formatter
public class UserInput
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);//Declares the Scanner in scan

System.out.println("Enter a number: ");
int num = scan.nextInt();//so scan.nextInt takes in an integer and stores it in num

scan.nextLine();//takes in a string

System.out.println("What is your name?");
String name = scan.nextLine();//takes in the string and stores it in name

System.out.println("Enter a decimal:");
double dec = scan.nextDouble();//takes in the decimal and stores it in dec

System.out.println("num = " + num + ", name = " + name + ", and dec = " + dec);//prints out all the things that were entered by the user


DecimalFormat decFormat = new DecimalFormat("0.00");//2.00, it will always put .00
//("0.##") will put 0.number
//("0000") = 0001 when 1 is entered

/*f2 = (float)4/5 = 0.8
f2 = (float)(4/5) = 0.0

import java.text.NumberFormat;
NumberFormat percent = NumberFormat.getPercentInstance();
*/
}
}


Ummm... so yea i'm not really sure what more to add because i'm not sure how advanced some of you are in Java so... let me know in the comments below if you would like me to clarify anything or add something new, or change something etc.
Last edited by primetime43 ; 03-08-2015 at 10:35 PM.

The following 8 users say thank you to primetime43 for this useful post:

Cyb3r, ImSooCool, Pac-Man, AFG, RTE, Smooth, Tory Lanez
03-15-2015, 04:42 AM #2
Rath
Today Will Be Different
Originally posted by primetime43 View Post
snip


Nice write up. Also, booleans are their own data type, I believe. :p

The following user thanked Rath for this useful post:

Tory Lanez
03-15-2015, 06:47 PM #3
primetime43
Knowledge is power Tiphat
Originally posted by Rath
Nice write up. Also, booleans are their own data type, I believe. :p


Yea they are, they're boolean data type.
03-18-2015, 12:51 AM #4
Mr Smithy x
Former Staff
Originally posted by primetime43 View Post
Hi there NGU! So today I am going to make this thread to help those who want to further their knowledge in Java. So to start out, I recommend using BlueJ, you can go to my other thread to get BlueJ here: You must login or register to view this content.

So if you clicked on the link to my other thread, you already know how to make a class and print out in the console. So first off we will start with the basic primitive data types of java.
    
[U]Integer Types (from smallest to largest)[/U]
byte
short
int
long
[U]Floating Point Types[/U]
float
double
[U]String Types
[/U]char
String

And then also there is the boolean, not sure what type it is though.

So you want to use an integer type to store a whole number, a floating point type to store a decimal, and use the string types to store a string.


So in your main class, you're gonna want to use this
     public static void main(String[] args) 
{
//code in here
By the way, two slashes like this // is a comment and will not be read as code by your program. You can also do a multi - line comment by doing this

/*
* Comment in here
* etc
*/

}


So inside the main above, you may want to print something out in the console, to do so, you do this:
    
System.out.println(""); //prints out whatever string is inside the "". The println means it will print on that line. Which means it will go to a new line each time
System.out.print(""); //Just using print means it prints out and does not go to a new line



Now somethings you can use also in printing things out are
    
\n - new line
\t - tab
\b - backspace/r - return
\' - Inserts a single quote
\" - Inserts a double quote
\\ - Inserts a backslash


Sample code with using strings etc:
    
public class FunWithString
{
public static void main(String [] args)
{
System.out.println("This is a String"); //String is an object data type

System.out.print("Roses are Red, ");
System.out.println("Violets are Blue, ");
System.out.println("\tbut I have a \"committment\" issues");
System.out.println("so I think we should be friends, \nat this point in our relationship\\journey.");


System.out.println("This is concatenation " + 24 + 45);
System.out.println("This is adding " + (24+45));
int num1 = 10;
System.out.println("The total is: " + num1 + " boxes.");

String s1 = "The total is: ";
String s2 = "Banana";
String s3 = "Apple";
String s4 = "Orange";
String s6 = "Apple";

System.out.println(s2 + s3);
System.out.println(s3.concat(s4));
System.out.println(s4.length());
System.out.println(s4.substring(1,4));
String s5 = s4.substring(1, 2);
System.out.println(s5);
char c1 = s4.charAt(1);
System.out.println(c1);
System.out.println(!(s4.equals(s6)));

s4 = s4.toUpperCase();

s3 = s3.toLowerCase();

}
}


Here is some sample code with A LOT of NOTES in it so make sure you read them! :
    
public class Class1
{//Start class
public static void main (String[] args)
{//Start main
System.out.print("Hellow World!");
System.out.println("Yay!");


//Integer Data Type - Primitive Data type
// 4 integer data types - byte, short, int, long ///////////////////////////////
byte b1; //assigned
b1 = 0; //initialized //256
short s1 = 0; //assigned and initialized
int num1 = 0;
long dig1 = 0;


int num2 = 0, num3 = 4, num4 = 7;
byte b2, b3 = 10, b4 = 1;


int dayOfWeek = 1; //good variable name
int totalPay = 0;
int NUMBER_OF_WIDGETS = 0;


//Floating Point Variables - Primitive Data Types //////////////////// float, double
float f1 = 0.0F;
double d1 = 0.0D;


int iResult = 0;
double dResult = 0.0;


iResult = 4/3;
dResult = 4/3; //assignment conversion
System.out.println(iResult);
System.out.println(dResult);


dResult = 4.0/3; //Arithmetic Promotion


float fResult = (float)4.0/(float)3.0;
iResult = (int)(4.0/3.0); //casting


System.out.println(dResult);


System.out.println(4.0/3.0);
System.out.println(4.0F/3.0F);
num2 = 4;
num2++; //num 2 = num2 + 1;
System.out.println(num2);
System.out.println(++num2);


System.out.println(num2--);
System.out.println(num2);
System.out.println(--num2);


/*Operations on integers: +, -, *, /, %, ++, --, +=, -= , *=, /= */


num2 = 5;
num2*=3; //num2 = num2 * 3;
System.out.println(num2);


num2-=7; //num2 = num2 - 7;
System.out.println(num2);


num2 %= 3;
System.out.println(num2);


float f2 = 0.0F;
f2 = 1.0F;
System.out.println(f2);


//Primitive Data Type: boolean


boolean flag = true;
boolean bool1 = false, boo2;


/*boolean operators: == "equal to"
* != "not equal"
* && "and"
* ! "not"
* || "or"
* > "greater than"
* < "less than"
* <= "less than or equal to"
* >= "greater than or equal to"
*/


//Primitive Data type: character
char c1 = 'a';
char c2 = 'A',c3,c4='#';


//Operations with characters: <, >, <=, >=, =, !=


System.out.println('a'=='A'Winky Winky;
}//End main
}//End class
//End file


So if you want to ever have some user input, you can use a scanner. Now to use scanner you have to import it and declare it. Also ill show how to use the formatter.
    
import java.util.Scanner;//imports the scanner class
import java.text.DecimalFormat;//imports the decimal formatter
public class UserInput
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);//Declares the Scanner in scan

System.out.println("Enter a number: ");
int num = scan.nextInt();//so scan.nextInt takes in an integer and stores it in num

scan.nextLine();//takes in a string

System.out.println("What is your name?");
String name = scan.nextLine();//takes in the string and stores it in name

System.out.println("Enter a decimal:");
double dec = scan.nextDouble();//takes in the decimal and stores it in dec

System.out.println("num = " + num + ", name = " + name + ", and dec = " + dec);//prints out all the things that were entered by the user


DecimalFormat decFormat = new DecimalFormat("0.00");//2.00, it will always put .00
//("0.##") will put 0.number
//("0000") = 0001 when 1 is entered

/*f2 = (float)4/5 = 0.8
f2 = (float)(4/5) = 0.0

import java.text.NumberFormat;
NumberFormat percent = NumberFormat.getPercentInstance();
*/
}
}


Ummm... so yea i'm not really sure what more to add because i'm not sure how advanced some of you are in Java so... let me know in the comments below if you would like me to clarify anything or add something new, or change something etc.


Nice tutorial i'm thinking of making a thread for android and linking this thread for the foundations of java types & foundation

The following user thanked Mr Smithy x for this useful post:

primetime43

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo