2

i am new to stackoverflow and recently interested in android app design. I was a C programmer for mcu coding so not familiar with java syntax.

In C or some compiler i used before, they support a syntax called "#define", which i can put a statement into it like:

#define (tried && sleepy)   BAD_MOOD
#define (happy && joy)      GOOD_MOOD

To be used like:

if (GOOD_MOOD)
  GO_TO_WORK
else if (BAD_MOOD)
  SICKLEAVE
else
  ...

Does java have some syntax like this? or alternatively, should i define many boolean variable instead?

2
  • should i continue my view on that existing question? Commented Feb 24, 2017 at 6:57
  • One other thing: if you consider posting here more often, spend some time at the help center; and learn what / how to ask here ... Commented Feb 24, 2017 at 11:36

1 Answer 1

0

Java does not have #define; or any other kind of macro support; as it does not even have the concept of pre processing.

Given your comments: first of all, you really want to step back. You don't learn Java by applying your C knowledge. Period. There is many existing documentation out there that summarizes the important differences between C and Java; you start by searching and reading such stuff.

Then: the real difference is that Java is a (heavily, but not 100%) object oriented programming language; and C is not. So you not only change syntax elements; you apply different concepts. One first starting point here would be the SOLID principles.

And then there are subtle things, such as Java coding style guides - that instruct for simple things as "no _ chars in variable names" (except for SOME_CONSTANT).

And regarding your example, a simple example would look like:

public class Class {
  private final static int MAX_COUNT_OF_STUDENTS = 50;

  private int numberOfStudents;  
  private int numberOfBooks;

  public void setNumberOfStudents(int newCount) {
    numberOfStudents = newCount;
  }

  public boolean isFull() {
    return ( numberOfStudents <= MAX_COUNT_OF_STUDENTS );
  }
  ...

That is how you would approach such things in Java. There key thing is to design classes that represent the objects in the real world you intend to model; to add the required behavior as methods.

And beyond that: even in the C world I would be really really cautious about using #defines this way. Yes, they are convenient and have their place; but using them just to safe typing word like in your example I would consider a bad practice.

Sign up to request clarification or add additional context in comments.

4 Comments

so do u think making numbers of boolean variable is a good way? i.e. boolean class_is_full = false; boolean books_are_ready = false; boolean start_the_class = false; if (number_of_students == 50) class_is_full = true; if (number_of_books >= 50) books_are_ready = true; if(class_is_full && books_are_ready) start_the_class = true; basically, i was wanna make my code looks understandable.
I enhanced my answer; but just for the record: don't come back with more questions. You are at a very basic level; and that requires you doing studying. And this here is not a place were you get teaching for free ;-)
thank you for your time to demonstrate the basic based on my example. I understand some (not all) different between C and java. The SOLID principles sounds reasonable but hard for me to follow. but anyways, that's where I should start with. thanks again.
You are very welcome1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.