0% found this document useful (0 votes)
66 views

COMP1AL Final Notes

This document provides an overview of key concepts in the Java programming language including keywords, primitive data types, operators, comments, identifiers, strings, characters, and examples of programs to check for palindromes and classify characters. It covers Java syntax elements like separators and punctuators. It also describes numeric types like byte, short, int, long, float, double and the boolean type.

Uploaded by

Joyce Dacanay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

COMP1AL Final Notes

This document provides an overview of key concepts in the Java programming language including keywords, primitive data types, operators, comments, identifiers, strings, characters, and examples of programs to check for palindromes and classify characters. It covers Java syntax elements like separators and punctuators. It also describes numeric types like byte, short, int, long, float, double and the boolean type.

Uploaded by

Joyce Dacanay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Programming in Java

Java Keywords
Keyword type
Primitive types
Access modifiers
Special modifiers
Control flow
OOP specific

Keywords
boolean, byte, char, double, float, int, long, short
public, private, protected
abstract, final, native, static, strictfp, synchronized,
transient, volatile
if, else, do, while, switch, case, default, for, break,
continue
class, extends, implements, import, instanceof, interface,
new, package, super, this

Exception handling catch, finally, try, throw, throws


Method specific
return, void
Unused
const, goto
Other literals
true, false, null
Java Operators
unary operators
unary plus, unary minus
postfix/prefix increment
postfix/prefix decrement
bitwise complement
logical complement
multiplicative operators
multiplication
division
remainder
additive operators
addition, subtraction
string concatenation
shift operators
relational operators
equality operators
bitwise and logical operators
integer bitwise
conditional AND, conditional OR
conditional
assignment operators
simple assignment
compound assignment

+ ++
-~
!
*
/
%
+ +
<< >> >>>
> < >= <=
== !=
& ^ |
&& ||
?:
=
+= -= *= /= &= |=
^= %= <<= >>= >>>=

Java Separators and Punctuators


()
used to denote precedence of operations in expressions; used to delimit the parameter list in
method declarations and the argument list in method calls
{}
used to denote the scope of blocks; also used in initializers
[]
used for array indexing
;
statement terminator
,
list value separator
.
field/member access
"
delimiter for string literals
'
delimiter for character literals
other whitespace characters serve to separate Java program elements from one another; the whitespace
characters are the space, horizontal tab, form feed, line feed or newline, and the carriage return
Java Primitive Types
numeric types
integral
floating-point

byte short int long char


float double

boolean type

boolean

range of values
byte: -128 to 127 inclusive
short: -32768 to 32767 inclusive
int: -2147483648 to 2147483647 inclusive
long: -9223372036854775808 to 9223372036854775807 inclusive
char: '\u0000' to '\uffff' inclusive, i.e., from 0 to 65535
float and double, floating-point values as defined in the single-precision 32-bit and double
precision 64-bit IEEE 754 standard
boolean, logical values

Examples of Literal Values for the Java Primitive Types


int
0 2 0372 0xDadaCafe 1996 0x00FF00FF
long
0l 0777L 0x100000000L 2147483648L 0xC0B0L
float
1e1f 2.f .3f 0f 3.14f 6.022137e+23f
double
1e1 2. .3 0.0 3.14 1e-9d 1e137
char
'a' '%' '\n' '\t' '\\' '\'' '\u03a9' '\uFFFF' '\177' '' ''
boolean
true false
Java Comments
/* text */
traditional comment; all the text from the characters /* to the characters */ are ignored
// text
end-of-line comment: all the text from the characters // to the end of the line is ignored
/** text */
treated as a documentation comment which can be proccessed by the javadoc tool to
automatically generate documentation from Java source code files
comments do not nest, i.e., that is, the characters /* and */ have no special meaning in comments that
begin with //, and the characters // has no special meaning in comments that begin with /*
Java Identifiers
Java identifiers are used to name different program elements such as variables, constants, methods,
classes, interfaces, etc.
must start with a Java letter, i.e., the Latin letters A to Z, a to z, the underscore _, the dollar sign
$, and other Unicode alphabet characters
must consist only of Java letters and digits (0 to 9)
unconstrained in length, though for practical purposes should be short but descriptive enough
are case-sensitive, i.e., distinguishes between uppercase and lowercase characters
although the Java language does not require it, efforts should be exerted in writing Java program that
conform to de facto, or generally accepted, standard conventions; e.g., the following are usually
considered standard naming conventions for Java identifiers:
Pascal-case, i.e., uppercase first character, with capitalization on word boundaries, for class and
interface names; e.g., MyClass, HelloWorld, etc.
camel-case, i.e., lowercase first character, with capitalization on word boundaries, for variable
and method names; e.g., empSalary, theNumber, studentLastName, dropSubject, etc.
all uppercase characters, typically used for naming constants, e.g., PI, MAX_VALUE,
MIN_VALUE, etc.
String Methods
charAt(int index)
Returns the char value at the specified index.
compareTo(String anotherString)
Compares two strings lexicographically.
compareToIgnoreCase(String str)
Compares two strings lexicographically, ignoring case differences.
equals(Object anObject)
Compares this string to the specified object.
equalsIgnoreCase(String anotherString)
Compares this String to another String, ignoring case considerations.
indexOf(int ch)
Returns the index within this string of the first occurrence of the specified character.

length()
Returns the length of this string.
toLowerCase()
Converts all of the characters in this String to lower case using the rules of the default locale.
toUpperCase()
Converts all of the characters in this String to upper case using the rules of the default locale.
Character Methods
isLetter()
Determines whether the specified char value is a letter.
isDigit()
Determines whether the specified char value is a digit.
toUpperCase()
Returns the uppercase form of the specified char value.
toLowerCase()
Returns the lowercase form of the specified char value.
toString()
Returns a String object representing the specified character value that is, a one-character string.
No Vowels
Write a Java program that accepts a string value as input and then prints out the input string with the vowels removed.
Sample Output:

Input Value: Long Live Saint Louis


University!!!
Lng Lv Snt Ls nvrsty!!!

Input Value: crypt


crypt

public class NoVowels {


public static boolean isVowel(char ch) {
String s = "aeiouAEIOU";
return (s.indexOf(ch) >= 0);
}
public static void main(String[] args) {
String st = Keyboard.readString();
String stnv = "";
for (int i = 0; i < st.length(); i++) {
if (!isVowel(st.charAt(i))) {
stnv += st.charAt(i);
}
}
System.out.println (stnv);
}
}
Palindrome 1 (NumPalSt)
Write a Java program that accepts as input an integer value, and then determines whether or not the input integer is a
palindrome (i.e., if it is the same read forwards and backwards; e.g., 346643 or 12321). If the input number is not a
palindrome, the program then determines whether the reverse of the input value (i.e., the input value with its digits
reversed) is less than or greater than the original input value.
Sample Output:

Input Value: 236909632

Input Value: 12345

Input Value: 673

Palindrome: Yes

Palindrome: No
Reverse Value: Greater

Palindrome: No
Reverse Value: Less

public class NumPalSt {


public static void main(String[] args) {
System.out.print("Input value: ");
int n = Keyboard.readInt();//10

String numSt = Integer.toString(n);//"10"


String numRev = "";
for (int i = numSt.length()-1; i >= 0; i--) {
numRev += numSt.charAt(i);
}

System.out.print ("Palindrome: ");


if (numSt.equals(numRev)){
System.out.println ("Yes");
} else {
System.out.println ("No");
System.out.print ("Reverse Value: ");
if (n > Integer.valueOf(numRev))
System.out.println ("Less");
else
System.out.println ("Greater");
}

}
Vowels and others
Write a Java program which accepts as input a character value, and then determines if the input character is a digit, a
vowel, or a consonant character. Assume that only such characters are inputted, and that, for alphabetic characters,
the input is in lowercase.
Sample Output:

Input Character: 6

Input Character: k

Input Character: u

Character is a DIGIT.

Character is a
CONSONANT.

Character is a VOWEL.

public class CheckLetters {


public static boolean isVowel(char ch) {
String s = "aeiouAEIOU";
return (s.indexOf(ch) >= 0);
}
public static void main(String[] args) {
System.out.print("Input a character: ");
char c = Keyboard.readChar();
if (Character.isDigit(c)){
System.out.println("character is a digit");
} else {
if (Character.isLetter(c)) {
if (isVowel(c)) {
System.out.println("character is a vowel");
} else {
System.out.println("character is a consonant");
}
}
}
}

You might also like