Skip to content

Instantly share code, notes, and snippets.

View rajeevprasanna's full-sized avatar

Rajeev Kumar kallempudi rajeevprasanna

View GitHub Profile
@rajeevprasanna
rajeevprasanna / Frog.java
Created February 1, 2014 16:07
Static variable example
package staticExample;
public class Frog {
static int frogCount = 0;
public Frog(){
frogCount += 1;
}
public static void main(String[] args){
@rajeevprasanna
rajeevprasanna / Foo.java
Created February 1, 2014 16:11
tatic method can’t access a nonstatic
package staticExample;
public class Foo {
int x = 3;
public static void main (String [] args) {
System.out.println("x is " + x);
}//“Hey, I have no idea which Foo object’s x variable you’re trying to print!”
//Remember, it’s the class running the main() method, not an instance of the class.
}
@rajeevprasanna
rajeevprasanna / Animal.java
Created February 1, 2014 16:18
Static method overriding
package staticExample.staticOverride;
public class Animal {
static void doStuff() {
System.out.print("a ");
}
}
@rajeevprasanna
rajeevprasanna / Test1.java
Created February 1, 2014 16:39
Double and float comparison
package floatAndDoubleComparison;
public class Test1 {
public static void main(String[] args) {
double a = 99999.8d;
double b = 99999.65d;
System.out.println(a + b);
float a2 = 99999.8f;
float b2 = 99999.65f;
@rajeevprasanna
rajeevprasanna / CharLiteral.java
Last active August 29, 2015 13:55
character literal out of range
package charLiteral;
//Refer : https://gist.github.com/rajeevprasanna/8755247
public class CharLiteral {
public static void main(String[] args) {
char a = 0x892;
System.out.println(a);// ?
char b = 982;
@rajeevprasanna
rajeevprasanna / TestX.java
Created February 1, 2014 17:23
Implicit int casting
package implicitIntCating;
public class TestX {
public static void main(String[] args) {
byte a = 3;
byte b = 4;
byte c = (byte) (a + b);
System.out.println(c); // 7
@rajeevprasanna
rajeevprasanna / ReferenceTest.java
Created February 1, 2014 17:49
passing primitive values to methods
package variablePassing;
public class ReferenceTest {
public static void main(String[] args) {
int a = 1;
ReferenceTest rt = new ReferenceTest();
System.out.println("Before modify() a = " + a);
rt.modify(a);
System.out.println("After modify() a = " + a);
@rajeevprasanna
rajeevprasanna / InstanceVariableShadowing.java
Created February 1, 2014 17:52
InstanceVariableShadowing
package variablePassing;
public class InstanceVariableShadowing {
static int size = 7;
static void changeIt(int size) {
size = size + 200;
System.out.println("size in changeIt is " + size);
}
@rajeevprasanna
rajeevprasanna / ShadowReferenceVariables.java
Last active August 29, 2015 13:55
ShadowReferenceVariables
package variablePassing;
// Refer : https://gist.github.com/rajeevprasanna/8755845
public class ShadowReferenceVariables {
public Bar myBar = new Bar();
void changeIt(Bar myBar) { //myBar is copy reference pointing to inst1
myBar.barNum = 99; //inst1 bar variable is modified
System.out.println("myBar.barNum in changeIt is " + myBar.barNum); //99
@rajeevprasanna
rajeevprasanna / ArrayInitialization.java
Created February 1, 2014 18:47
ArrayInitialization
package arrays;
public class ArrayInitialization {
public static void main(String[] args) {
// Declare and create an array holding three references to int arrays
int[][] scores = new int[3][];
// the first element in the scores array is an int array of four int elements