Reserved words in java

Reserved words in java

Reserved words are keywords that the language uses to determine the properties of something. For example all of the datatypes, static , void, and abstract are reserved words. So while you can say put these words in a string, you can’t make the name of a variable, method, constructor or anything similar into one. They are also split into keywords and literals( like true and false). The list of them and a short description of what they do(from what I know) is below. I may have missed some since there is a lot and some I have never used. They are also all lowercase I am just to lazy to go and correct all of it.

Abstract- used in a method or class declaration and stops either from being used to make instances

Assert- used with assertion statements( whatever those are)

Boolean- a datatypes that uses true and false as values

Break- used to jump out of one set of parenthesis with if and switch statments

Byte- primitive datatypes can hold -128 to 127

Case- used in switch statments

Catch-used when looking for errors along with try

Char- datatypes where it’s a single letter that is given a number value

Class- used with declaring classes

Const- I don’t think it’s really used

Continue- used in a loop to jump to the next iteration of a loop

Default-declares default values and allows interface to provide the default of a method

Do- used in do while

Double- dataype to use for fractions and decimals Max and min is 17 followed by 307 zeros

Else- used in if else statments

Enum-dataype that can hold constants methods and more(I’ve never used this one either)

Extends- used when making a subclass

False-boolean dataype value

Final- used when making constants

Finally-follows a catch or try used to do things like ( I don’t really remember doing this)

Float- datatype for numbers the range is (2 to the-63) to ((2 to the 63)-1)

For- used in for loops

Goto- not used really

If- if statements

Implements- used when making a class inherit a interface

Import- used in import statement which are used to take in Java libraries

Instanceof -used to test if variable is of a specific type returns true or false( never had to use it)

Int- datatype USD with integer vslues

Interface- used when declaring interfaces

Long- another datatype got numbers min is 0 max is (2 to the 64) -1

Native- allows for Java to be called by other languages( I never had to do this)

New- used to initiate a new instance so like when declaring objects

Null- used to make sure there is actually a valid reference to a object

Package- used to group classes together

Private- used to make sure outside classes can’t access something( except for classes in the same package I think)

Protected- similar to private but allows for subclasses and package classes to use it ( I didn’t know this existed)

Public- the opposite of private

Return- used in methods to return a value to where the method was called

Short- datatype for numbers min -32,768 max 32,767

Static- used to make something apply to all instances

Strictfp- it supposedly restricts decimal calculations (what??????)

Super- used to call superclasses stuff

Switch- switch statement

Synchronized- prevents multiple pieces of code from accessing a certain piece of code at the same time( I never used it , but most of my programs it wouldn’t be a issue in the first place. My programs are too simple.)

this- used to reference current object

Throw- used to cause exeptions

Throws-used to list exeptions( I don’t do much with exeptions)

Transient- marks a variable to not be serialized, so the variable won’t be saved in a particular file ( umm I never used it but I googled it so …)

True- Boolean dataype value

Try- used with exeptions to make sure they don’t crash your program

Void- used to signify that there is no return in a method

Volatile- makes a variable stored in main memory ( so it’s ” thread-safe” apparently)

While – used on whole and do-while loops

objects

objects

A object is essentially the instance of a class. So alternatively a class is like the blueprint of a object.A object will have attributes and behaviors, like one would in real life.

A attribute would be like the color, length width and weight of iteam. This is typically represented through the parameters used when making a object( which are passes into the constructor and turned into fields/ global variables).

Behavior is the things the object can do. For example a tea kettle can boil water so a tea kettle object would have a method it could call to boil the water.

Creating a object:

ClassName objectName = new ClassName(<parameter>);

So when creating a object you need to declare what you will call it create the object with the new operator and call the constructer which initializes the object, similar to how you make a variable.

Calling a method:

Variablename = ObjectName.methodName(<parameter>);

Of course you can also declare a variable at the same time as calling the method and of the method is void you don’t set a variable to it.

Calling a field:

Most of the time you should have a get or set method but if you don’t . . .

objectName.varableName

Either in a system.print, parameter or setting a variable.

Interface

Interface

A interface is like a class where non of the methods have a body(called a abstract method) so a common analogy that is used for interfaces is that it’s like the blueprint of a class. Which basically means that tells what a class has to do but not how.So the main purpose of a interface is either to use polymorphism more effectively or to have interaction between to classes be based on the interfaces abstract methods which one of the classes implements since if the class implements the interface then it must define the methods given otherwise it becomes a abstract class and Java doesn’t support objects of abstract classes. (So by implementing the interface, the class is essentially promising that it will define the methods in the interface in that class or a subclass of that class)

A interface is made as shown below :

public interface interfaceName

{

returntype methodName(type parametername);

}

You can also make variable constants, but this isn’t recommended because the constants of different interfaces if given the same name and implemented by the same class could cause the program not to know which to use and cause a error.

In JDK8 and JDK9 there have been some updates to interface for more info on that you can look at the geek for geek article on interfaces( in case you didn’t realize it I’m not a professional so I do actually try to double check that my info is right before posting a article.)

Or you can wait till I add that information to here.

Some related topics are:

inheritance:

classes(not done yet):

Inheritance

Inheritance

Inheritance is where one class extends another or implements a interface, so in the case of extension it can call that classes methods, without making a object of the class. Other benefits include being able to use the superclass as a general class so you can have a parameter include the superclass and all subclasses of the superclass.

For example:

public class Balloon

public class RoundBalloon extends balloon

public class SquareBalloon extends balloon

public void draw(balloon k)

{

}

could take Balloon, SquareBalloon and/or RoundBalloon.

The drawback of inheritance in Java is that only one superclass can be extended so a class can’t inherit from multiple classes. However several interfaces can be implemented at the same time to (some extent) mimic multiple inheritance.

Related links are below:

Interfaces:

classes(not done yet):

Break vs continue

Break vs continue

Both of these reserved words are used in loops to end the current iteration of the loop often before getting to the end of the statements in the loop. They are also most often used inside of a if statement.

Break is used to end the loop before the loop would consider the condition false. So when the break statement is used it will end the loop and give control of the program to the statement right after the end of the loop. Break is also common to see inside of switch statements.

Continue is used to end the current iteration of the loop. So when it’s used it will disregard the rest of the loop that comes after it and go back to the conditional statement, and run the loop again as normal.

Ex:

for (int I =0; I<5; I ++)

{

if (j=3 && I=<2)

continue;

else if (j=3)// I >2

break;

else

j-1;

}

system.out.print(j);

So in this loop if j meets the conditions of the if statement then it will print out a 1. While if the conditions of the else if statement are met it will print out a 3. While if neither of them are met the answer it will print out will just be j(before the loop started)-5.

Frontpage

Frontpage

This blog is about programing and just computers in general. I made this blog for the 20x project my English teacher had my class do and I am planning on continuing it. The posts will include some short descriptions of jobs that require programming/in-depth knowledge of computers or where it would be helpful. As well as information on specific languages or concepts involving technology.

If you notice that I either don’t clarify something properly have wrong/out-of-date information or am missing information please comment on the the post so I know since I am not a professional although I did take a course on programming at my school and I do try to double check my information before I post something.

Side note: I won’t be doing videos for a while because I was using a Chromebook from my school since, I don’t have a microphone that I can use with my computer.

HTML links

HTML links

HTML links are made with the <a> tag and the href attribute. So you would make a link by doing something like <a href=”somehyperlink”> something something</a>.  The text outside of the tags is what you can see of the hyperlink. You can change the default color of the link using CSS. You can also change here the link will be opened by using the target attribute You can make images into a link, and there are bookmark links that allow you to go to a specific part of the web page. Often you will put the links into navigation menues and things.

For more specific information I would suggest going to the link below which is where I found most of this information:
https://www.w3schools.com/html/html_links.asp

Sorting methods

Sorting methods

Sorting methods are ways to sort arrays in either ascending or descending order.WE sort arrays so they are easier to use. You  also use sorting methods to match two data sets, or presenting something in a orderly way.When sorting array we often use equals compareTo and/or compare methods. Also O( . . .) stands for time it takes for the program to sort in the worst case.

Merge-In merge sort you first make sure that there is more than one element if there is only one don’t do anything, if there are two swap them if you need to , if there are more than two split the array into to approximately equal halves, sort the first half of the array and the second half, and then merge the two halves back into one sorted array. Recursion is often used with this sorting method since it has a base case (when there is only one or two elements). It’s a O(n^2)

Quick-in quick sort you pick one element to be the pivot and then you sort around that pivot so that to the left are smaller elements and to the right are bigger elements. THen apply recursion to the right and left halves of the array.  It’s a O(n log n) algorithm.

Selection- In a selection sort you initialize a variable to the size of the array, find the largest element among the n elements, subtract one from n and repeat those steps except for initialization until n is less than or equal to 2.It’s a O(n^2) algorithm.

Insertion-In insertion sort you make n equal  to 1. Save the next element and find the place to insert it among the first n, shift elements as necessary and insert the saved one in the vacant slot, increment n by 1 and repeat steps except initialization until n is greater than array length. It’s a O(n^2) algorithm.

ArrayLists

ArrayLists

ArrayLists are similar to arrays but they will automatically increase the arrays capacity if you try to add a element to a already full arrayList. There are two important variables for arraylists: capacity and size. Capacity is the length of the current arraylist while size is the number of elements in the arraylist. A arrayList will always  hold a object. So if you want the arrayList to hold numbers you would use the Integer or Double object types. But you can add a primitive datatype to the list so long as that value is of the wrapper class that the arrayList has as its objectType. So like with ArrayList<Double> it will take 5.0 but it will not take 5 because 5 is a int not a double.

So you declare it in a field like:

private/public ArrayList<objectType> listName;

You declare it in  a method or constructor like:

ArrayList<objectType> listName = new ArrayList<objectType>();

You have to use the entire  ArrayList<objectType> whenever you are declaring variables, parameters, return values and when making one. Also the default capacity of a ArrayList is 10, which can be changed when declaring it by putting a whole number into the parenthesis.

The indices like Arrays start at zero and in order to get the size you use listName.size(), which as you can tell by the parenthesis is a method. A arraylist may use for each loops and when using methods that change the size of a array you have to be careful that you don’t accidentally skip any elements since if you delete a element the size will automatically decrement. Also a arrayList holds references to objects, and it can hold objects that are equal to each other and several references to the same object. Which means that the object can change after its been added to a Arraylist and the same object can belong to different arraylists.

A link to related topics is below:

For each loop;

Common Arraylist methods:

Arrays:

common arrayList methods

common arrayList methods

int size(): returns the number of elements in a array.

Boolean isEmpty():returns  true if list is empty falsi if it isn’t.

Boolean add(objectType  element) : returns true and adds element to the end of the list

Void add( int i, E element):insert element at ith position and shifts elements to fit it( increments the elements at i and after by 1.

ObjectType get(int i) : returns value if ith element

ObjectType set(int i, ObjectType element): replaces the ith element and returns the old value.

ObjectType remove (int i) removes ith element and returns its value.

Boolean contains (Object obj) returns true if the list has the object (equals method used for comparison).

Int indexOf(Object obj) returns index of the first occurence of obj in this arraylist, or -1 if it isn’t found.

String toString() returns a string representation of the list.

A link to the full list of refrences is below:

ArrayList methods (Oracle)