Computer Engineering Study
Tuesday, October 5, 2010
+ (addition)
- (subtraction)
* (multiply)
/ (division)
% (modulus)
++ (increment) x++ x = x + 1;
-- (decrement) x-- x = x – 1;
Lets See a program:
class test
{
public static void main (String args[])
{
byte b = 12 * 6;
System.out.println (b);
int i1 = 12 + 13;
System.out.println (i1);
int i2 = 12 - 13;
System.out.println (i2);
int i3 = 12 / 13;
System.out.println (i3);
double d = 12d / 13d;
System.out.println (d);
int i4 = 12 % 3;
System.out.println (i4);
int i5 = 12 % 5;
System.out.println (i5);
}
}
Result is
72
25
-1
0
0.9230769230769231
0
2
Shortcut Operators
k = k + 2 k += 2
k = k – 2 k -= 2
k = k * 2 k *= 2
k = k / 2 k /= 2
k = k % 2 k %= 2
Monday, August 9, 2010
Naming convention of Variable
Variables:
Variables should be initialized where they are declared and they should be declared in the smallest scope possible.
This ensures that variables are valid at any time. Sometimes it is impossible to initialize a variable to a valid value where it is declared. In these cases it should be left uninitialized rather than initialized to some phony value.
Variables must never have dual meaning.
Enhances readability by ensuring all concepts are represented uniquely. Reduce chance of error by side effects.
C
lass variables should never be declared public.
The concept of Java information hiding and encapsulation is violated by public variables. Use private variables and access functions instead. One exception to this rule is when the class is essentially a data structure, with no behavior (equivalent to a C++ struct). In this case it is appropriate to make the class' instance variables public.
Arrays should be declared with their brackets next to the type.
double[] vertex; // NOT: double vertex[]; int[] count; // NOT: int count[]; public static void main(String[] arguments) public double[] computeVertex()
The reason for is twofold. First, the array-ness is a feature of the class, not the variable. Second, when returning an array from a method, it is not possible to have the brackets with other than the type (as shown in the last example).
Variables should be kept alive for as short a time as possible.
Keeping the operations on a variable within a small scope, it is easier to control the effects and side effects of the variable.
Variables:
Variables should be initialized where they are declared and they should be declared in the smallest scope possible.
This ensures that variables are valid at any time. Sometimes it is impossible to initialize a variable to a valid value where it is declared. In these cases it should be left uninitialized rather than initialized to some phony value.
Variables must never have dual meaning.
Enhances readability by ensuring all concepts are represented uniquely. Reduce chance of error by side effects.
Class variables should never be declared public.
The concept of Java information hiding and encapsulation is violated by public variables. Use private variables and access functions instead. One exception to this rule is when the class is essentially a data structure, with no behavior (equivalent to a C++ struct). In this case it is appropriate to make the class' instance variables public.
Arrays should be declared with their brackets next to the type.
double[] vertex; // NOT: double vertex[]; int[] count; // NOT: int count[]; public static void main(String[] arguments) public double[] computeVertex()
The reason for is twofold. First, the array-ness is a feature of the class, not the variable. Second, when returning an array from a method, it is not possible to have the brackets with other than the type (as shown in the last example).
Variables should be kept alive for as short a time as possible.
Keeping the operations on a variable within a small scope, it is easier to control the effects and side effects of the variable.
Wednesday, October 28, 2009
Data types of Java
A. Integer Types (All signed)
1. byte 8 bit
2. short 16 bit
3. int 32 bit
4. long 64 bit
B. Floating Point Types (All signed)
5. float 32 bit
6. double 64 bit
C. Boolean Type
7. boolean true / false
D. Char Type (unsigned)
8. char 16bit
Literal
Literal is a primitive typed values that appears directly inside a program.
int i = 20;
long l = 1000000;
double d = 5.0;
Floating point literals (float & double) are written with a decimal point. By default, a floating-point literal is treated as a double type.
class test
{
public static void main (String args[])
{
float i = 9.8;
}
}
Result of compilation:
G:\work>javac test.java
test.java:5: possible loss of precision
found : double
required: float
float i = 9.8;
class test
{
public static void main (String args[])
{
float i = 9.8F; // F or f
}
}
OK to compile
class test
{
public static void main (String args[])
{
int i = 2200;
int hex = 0x2200; // Starts with 0x
int oct = 02200; // Starts with 0, Binary literal not
// possible
long l = 1000000L;
boolean done = true;
char cr = 'A';
String str = "Abc";
if ( hex > 0x2200 ) { // Do something }
if (done) { // Do something }
if ( cr == 'B') { // Do something }
}
}
Monday, August 17, 2009
Java
Keywords |
Keywords are language specific and cannot be used for any other purpose other than they are intended for. Example of Java keywords: class, int, public, static, void etc. |
Identifiers |
Identifiers are used to identify classes, variables, constants etc. Identifiers must start with a letter Class 1 // NOT allowed {} class One // OK {} |
Separators |
() used to contain lists of parameters in method, surrounding cast types, expressions in control statement {} defining a block of code for classes, methods etc [ ] used to declare array types ; terminating statements , separate identifiers, chain statements inside for statement . used to indicate a method or variable in a object |
Sunday, August 9, 2009
Java
Now we can start coding with java. First of all we need a java compiler. JDK is most uses compiler . JDK (JAVA Development Kit) have a power full compiler and a debugger so any one can find what worng on program is. Just buy it from Market.
Look this Program:
Program1:
class Program1
{
public static void main(String args[])
{
System.out.println(“This is for displaying any variable or string”);
}
}
It’s a very simple java program.
Here you will find all of your needed information. Note Java is Object oriented Programming language It also a class based program every program start with class. Write down all code in any text editor(like notepad).
NOTE: Your java program Name must be name of your main class name. That’s mean look the program main class /*where main function*/ it’s class name is program1 so this program name is Program1.java. First letter of class name must be alphabetic .
now look the picture here every thing will be explain.
Keyword is special words which do some extra work. Those word Have some extra function and work. Your can use keyword for special purpose. Just mention That you can’t use keyword as statement, variable or any other use. "class” is a key word which explain a Class.
Friday, August 7, 2009
JAVA
Java is most demandable Programming language for it portability all over the world. From any were and any computer you can access java program. It is a great portability functions. Look this picture. From Bangladesh anyone write Mypro.java then just compile it with javac java compiler. The javac make a Mypro.class file which contain byte code for the program. If any one sends this to any where the people who receive it he can read the program. Program Controls by JVM (java virtual machine)./*We will talk about later.*/ So this Mypro.class is the main file that execute.